cg14_accel.c revision dbf8597c
1dbf8597cSmacallan/* $NetBSD: cg14_accel.c,v 1.18 2021/12/03 16:54:26 macallan Exp $ */
24261fa58Smacallan/*
34261fa58Smacallan * Copyright (c) 2013 Michael Lorenz
44261fa58Smacallan * All rights reserved.
54261fa58Smacallan *
64261fa58Smacallan * Redistribution and use in source and binary forms, with or without
74261fa58Smacallan * modification, are permitted provided that the following conditions
84261fa58Smacallan * are met:
94261fa58Smacallan *
104261fa58Smacallan *    - Redistributions of source code must retain the above copyright
114261fa58Smacallan *      notice, this list of conditions and the following disclaimer.
124261fa58Smacallan *    - Redistributions in binary form must reproduce the above
134261fa58Smacallan *      copyright notice, this list of conditions and the following
144261fa58Smacallan *      disclaimer in the documentation and/or other materials provided
154261fa58Smacallan *      with the distribution.
164261fa58Smacallan *
174261fa58Smacallan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
184261fa58Smacallan * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
194261fa58Smacallan * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
204261fa58Smacallan * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
214261fa58Smacallan * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
224261fa58Smacallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
234261fa58Smacallan * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
244261fa58Smacallan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
254261fa58Smacallan * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
264261fa58Smacallan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
274261fa58Smacallan * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284261fa58Smacallan * POSSIBILITY OF SUCH DAMAGE.
294261fa58Smacallan *
304261fa58Smacallan */
31c88c16f8Smacallan
32c88c16f8Smacallan#ifdef HAVE_CONFIG_H
33c88c16f8Smacallan#include "config.h"
34c88c16f8Smacallan#endif
35c88c16f8Smacallan
364261fa58Smacallan#include <sys/types.h>
374261fa58Smacallan
384261fa58Smacallan/* all driver need this */
394261fa58Smacallan#include "xf86.h"
404261fa58Smacallan#include "xf86_OSproc.h"
414261fa58Smacallan#include "compiler.h"
424261fa58Smacallan
434261fa58Smacallan#include "cg14.h"
444261fa58Smacallan
45b8ad197aSmacallan//#define SX_DEBUG
464261fa58Smacallan
474261fa58Smacallan#ifdef SX_DEBUG
484261fa58Smacallan#define ENTER xf86Msg(X_ERROR, "%s>\n", __func__);
494261fa58Smacallan#define DPRINTF xf86Msg
504261fa58Smacallan#else
514261fa58Smacallan#define ENTER
524261fa58Smacallan#define DPRINTF while (0) xf86Msg
534261fa58Smacallan#endif
544261fa58Smacallan
554261fa58Smacallan#define arraysize(ary)        (sizeof(ary) / sizeof(ary[0]))
564261fa58Smacallan
574261fa58Smacallan/* 0xcc is SX's GXcopy equivalent */
584261fa58Smacallanuint32_t sx_rop[] = { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee,
594261fa58Smacallan		      0x11, 0x99, 0x55, 0xdd, 0x33, 0xbb, 0x77, 0xff};
604261fa58Smacallan
614261fa58Smacallanint src_formats[] = {PICT_a8r8g8b8, PICT_x8r8g8b8,
624261fa58Smacallan		     PICT_a8b8g8r8, PICT_x8b8g8r8, PICT_a8};
634261fa58Smacallanint tex_formats[] = {PICT_a8r8g8b8, PICT_a8b8g8r8, PICT_a8};
644261fa58Smacallan
65f71acd79Smacallanstatic void CG14Copy32(PixmapPtr, int, int, int, int, int, int);
66f71acd79Smacallanstatic void CG14Copy8(PixmapPtr, int, int, int, int, int, int);
67f71acd79Smacallan
684261fa58Smacallanstatic inline void
694261fa58SmacallanCG14Wait(Cg14Ptr p)
704261fa58Smacallan{
71fc473876Smacallan	int bail = 10000000;
72fc473876Smacallan	/* we wait for the busy bit to clear */
73fc473876Smacallan	while (((read_sx_reg(p, SX_CONTROL_STATUS) & SX_BZ) != 0) &&
74fc473876Smacallan	       (bail > 0)) {
75fc473876Smacallan		bail--;
76fc473876Smacallan	};
77fc473876Smacallan	if (bail == 0) {
78fc473876Smacallan		xf86Msg(X_ERROR, "SX wait for idle timed out %08x %08x\n",
79fc473876Smacallan		    read_sx_reg(p, SX_CONTROL_STATUS),
80fc473876Smacallan		    read_sx_reg(p, SX_ERROR));
81fc473876Smacallan	}
824261fa58Smacallan}
834261fa58Smacallan
844261fa58Smacallanstatic void
854261fa58SmacallanCG14WaitMarker(ScreenPtr pScreen, int Marker)
864261fa58Smacallan{
874261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
884261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
894261fa58Smacallan
904261fa58Smacallan	CG14Wait(p);
914261fa58Smacallan}
924261fa58Smacallan
934261fa58Smacallanstatic Bool
944261fa58SmacallanCG14PrepareCopy(PixmapPtr pSrcPixmap, PixmapPtr pDstPixmap,
954261fa58Smacallan		int xdir, int ydir, int alu, Pixel planemask)
964261fa58Smacallan{
974261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
984261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
994261fa58Smacallan
1004261fa58Smacallan	ENTER;
1014261fa58Smacallan	DPRINTF(X_ERROR, "bits per pixel: %d\n",
1024261fa58Smacallan	    pSrcPixmap->drawable.bitsPerPixel);
1034261fa58Smacallan
1044261fa58Smacallan	if (planemask != p->last_mask) {
1054261fa58Smacallan		CG14Wait(p);
1064261fa58Smacallan		write_sx_reg(p, SX_PLANEMASK, planemask);
1074261fa58Smacallan		p->last_mask = planemask;
1084261fa58Smacallan	}
1094261fa58Smacallan	alu = sx_rop[alu];
1104261fa58Smacallan	if (alu != p->last_rop) {
1114261fa58Smacallan		CG14Wait(p);
1124261fa58Smacallan		write_sx_reg(p, SX_ROP_CONTROL, alu);
1134261fa58Smacallan		p->last_rop = alu;
1144261fa58Smacallan	}
115f71acd79Smacallan	switch (pSrcPixmap->drawable.bitsPerPixel)  {
116f71acd79Smacallan		case 8:
117f71acd79Smacallan			p->pExa->Copy = CG14Copy8;
118f71acd79Smacallan			break;
119f71acd79Smacallan		case 32:
120f71acd79Smacallan			p->pExa->Copy = CG14Copy32;
121f71acd79Smacallan			break;
122f71acd79Smacallan		default:
123f71acd79Smacallan			xf86Msg(X_ERROR, "%s depth %d\n", __func__,
124f71acd79Smacallan			    pSrcPixmap->drawable.bitsPerPixel);
125f71acd79Smacallan	}
1264261fa58Smacallan	p->srcpitch = exaGetPixmapPitch(pSrcPixmap);
1274261fa58Smacallan	p->srcoff = exaGetPixmapOffset(pSrcPixmap);
1284261fa58Smacallan	p->xdir = xdir;
1294261fa58Smacallan	p->ydir = ydir;
1304261fa58Smacallan	return TRUE;
1314261fa58Smacallan}
1324261fa58Smacallan
1334261fa58Smacallanstatic void
134f71acd79SmacallanCG14Copy32(PixmapPtr pDstPixmap,
1354261fa58Smacallan         int srcX, int srcY, int dstX, int dstY, int w, int h)
1364261fa58Smacallan{
1374261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
1384261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
1394261fa58Smacallan	int dstpitch, dstoff, srcpitch, srcoff;
1404261fa58Smacallan	int srcstart, dststart, xinc, srcinc, dstinc;
1414261fa58Smacallan	int line, count, s, d, num;
1424261fa58Smacallan
1434261fa58Smacallan	ENTER;
1444261fa58Smacallan	dstpitch = exaGetPixmapPitch(pDstPixmap);
1454261fa58Smacallan	dstoff = exaGetPixmapOffset(pDstPixmap);
1464261fa58Smacallan	srcpitch = p->srcpitch;
1474261fa58Smacallan	srcoff = p->srcoff;
1484261fa58Smacallan	/*
1494261fa58Smacallan	 * should clear the WO bit in SX_CONTROL_STATUS, then check if SX
1504261fa58Smacallan	 * actually wrote anything and only sync if it did
1514261fa58Smacallan	 */
1524261fa58Smacallan	srcstart = (srcX << 2) + (srcpitch * srcY) + srcoff;
1534261fa58Smacallan	dststart = (dstX << 2) + (dstpitch * dstY) + dstoff;
1544261fa58Smacallan
1554261fa58Smacallan	/*
1564261fa58Smacallan	 * we always copy up to 32 pixels at a time so direction doesn't
1574261fa58Smacallan	 * matter if w<=32
1584261fa58Smacallan	 */
1594261fa58Smacallan	if (w > 32) {
1604261fa58Smacallan		if (p->xdir < 0) {
1614261fa58Smacallan			srcstart += (w - 32) << 2;
1624261fa58Smacallan			dststart += (w - 32) << 2;
1634261fa58Smacallan			xinc = -128;
1644261fa58Smacallan		} else
1654261fa58Smacallan			xinc = 128;
1664261fa58Smacallan	} else
1674261fa58Smacallan		xinc = 128;
1684261fa58Smacallan	if (p->ydir < 0) {
1694261fa58Smacallan		srcstart += (h - 1) * srcpitch;
1704261fa58Smacallan		dststart += (h - 1) * dstpitch;
1714261fa58Smacallan		srcinc = -srcpitch;
1724261fa58Smacallan		dstinc = -dstpitch;
1734261fa58Smacallan	} else {
1744261fa58Smacallan		srcinc = srcpitch;
1754261fa58Smacallan		dstinc = dstpitch;
1764261fa58Smacallan	}
1774261fa58Smacallan	if (p->last_rop == 0xcc) {
1784261fa58Smacallan		/* plain old copy */
1794261fa58Smacallan		if ( xinc > 0) {
1804261fa58Smacallan			/* going left to right */
1814261fa58Smacallan			for (line = 0; line < h; line++) {
1824261fa58Smacallan				count = 0;
1834261fa58Smacallan				s = srcstart;
1844261fa58Smacallan				d = dststart;
1854261fa58Smacallan				while ( count < w) {
1864261fa58Smacallan					num = min(32, w - count);
1874261fa58Smacallan					write_sx_io(p, s,
1884261fa58Smacallan					    SX_LD(10, num - 1, s & 7));
1894261fa58Smacallan					write_sx_io(p, d,
1904261fa58Smacallan					    SX_STM(10, num - 1, d & 7));
1914261fa58Smacallan					s += xinc;
1924261fa58Smacallan					d += xinc;
1934261fa58Smacallan					count += 32;
1944261fa58Smacallan				}
1954261fa58Smacallan				srcstart += srcinc;
1964261fa58Smacallan				dststart += dstinc;
1974261fa58Smacallan			}
1984261fa58Smacallan		} else {
1994261fa58Smacallan			/* going right to left */
2004261fa58Smacallan			int i, chunks = (w >> 5);
2014261fa58Smacallan			for (line = 0; line < h; line++) {
2024261fa58Smacallan				s = srcstart;
2034261fa58Smacallan				d = dststart;
2044261fa58Smacallan				count = w;
2054261fa58Smacallan				for (i = 0; i < chunks; i++) {
2064261fa58Smacallan					write_sx_io(p, s,
2074261fa58Smacallan					    SX_LD(10, 31, s & 7));
2084261fa58Smacallan					write_sx_io(p, d,
2094261fa58Smacallan					    SX_STM(10, 31, d & 7));
2104261fa58Smacallan					s -= 128;
2114261fa58Smacallan					d -= 128;
2124261fa58Smacallan					count -= 32;
2134261fa58Smacallan				}
2144261fa58Smacallan				/* leftovers, if any */
2154261fa58Smacallan				if (count > 0) {
2164261fa58Smacallan					s += (32 - count) << 2;
2174261fa58Smacallan					d += (32 - count) << 2;
2184261fa58Smacallan					write_sx_io(p, s,
2194261fa58Smacallan					    SX_LD(10, count - 1, s & 7));
2204261fa58Smacallan					write_sx_io(p, d,
2214261fa58Smacallan					    SX_STM(10, count - 1, d & 7));
2224261fa58Smacallan				}
2234261fa58Smacallan				srcstart += srcinc;
2244261fa58Smacallan				dststart += dstinc;
2254261fa58Smacallan			}
2264261fa58Smacallan		}
2274261fa58Smacallan	} else {
2284261fa58Smacallan		/* ROPs needed */
2294261fa58Smacallan		if ( xinc > 0) {
2304261fa58Smacallan			/* going left to right */
2314261fa58Smacallan			for (line = 0; line < h; line++) {
2324261fa58Smacallan				count = 0;
2334261fa58Smacallan				s = srcstart;
2344261fa58Smacallan				d = dststart;
2354261fa58Smacallan				while ( count < w) {
2364261fa58Smacallan					num = min(32, w - count);
2374261fa58Smacallan					write_sx_io(p, s,
2384261fa58Smacallan					    SX_LD(10, num - 1, s & 7));
2394261fa58Smacallan					write_sx_io(p, d,
2404261fa58Smacallan					    SX_LD(42, num - 1, d & 7));
2414261fa58Smacallan					if (num > 16) {
2424261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2434261fa58Smacallan					    	 SX_ROP(10, 42, 74, 15));
2444261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2454261fa58Smacallan					    	 SX_ROP(26, 58, 90, num - 17));
2464261fa58Smacallan					} else {
2474261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2484261fa58Smacallan					    	 SX_ROP(10, 42, 74, num - 1));
2494261fa58Smacallan					}
2504261fa58Smacallan					write_sx_io(p, d,
2514261fa58Smacallan					    SX_STM(74, num - 1, d & 7));
2524261fa58Smacallan					s += xinc;
2534261fa58Smacallan					d += xinc;
2544261fa58Smacallan					count += 32;
2554261fa58Smacallan				}
2564261fa58Smacallan				srcstart += srcinc;
2574261fa58Smacallan				dststart += dstinc;
2584261fa58Smacallan			}
2594261fa58Smacallan		} else {
2604261fa58Smacallan			/* going right to left */
2614261fa58Smacallan			int i, chunks = (w >> 5);
2624261fa58Smacallan			for (line = 0; line < h; line++) {
2634261fa58Smacallan				s = srcstart;
2644261fa58Smacallan				d = dststart;
2654261fa58Smacallan				count = w;
2664261fa58Smacallan				for (i = 0; i < chunks; i++) {
2674261fa58Smacallan					write_sx_io(p, s, SX_LD(10, 31, s & 7));
2684261fa58Smacallan					write_sx_io(p, d, SX_LD(42, 31, d & 7));
2694261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
2704261fa58Smacallan				    	    SX_ROP(10, 42, 74, 15));
2714261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
2724261fa58Smacallan				    	    SX_ROP(26, 58, 90, 15));
2734261fa58Smacallan					write_sx_io(p, d,
2744261fa58Smacallan					    SX_STM(74, 31, d & 7));
2754261fa58Smacallan					s -= 128;
2764261fa58Smacallan					d -= 128;
2774261fa58Smacallan					count -= 32;
2784261fa58Smacallan				}
2794261fa58Smacallan				/* leftovers, if any */
2804261fa58Smacallan				if (count > 0) {
2814261fa58Smacallan					s += (32 - count) << 2;
2824261fa58Smacallan					d += (32 - count) << 2;
2834261fa58Smacallan					write_sx_io(p, s,
2844261fa58Smacallan					    SX_LD(10, count - 1, s & 7));
2854261fa58Smacallan					write_sx_io(p, d,
2864261fa58Smacallan					    SX_LD(42, count - 1, d & 7));
2874261fa58Smacallan					if (count > 16) {
2884261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2894261fa58Smacallan					    	    SX_ROP(10, 42, 74, 15));
2904261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2914261fa58Smacallan					    	 SX_ROP(26, 58, 90, count - 17));
2924261fa58Smacallan					} else {
2934261fa58Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
2944261fa58Smacallan					    	 SX_ROP(10, 42, 74, count - 1));
2954261fa58Smacallan					}
2964261fa58Smacallan
2974261fa58Smacallan					write_sx_io(p, d,
2984261fa58Smacallan					    SX_STM(74, count - 1, d & 7));
2994261fa58Smacallan				}
3004261fa58Smacallan				srcstart += srcinc;
3014261fa58Smacallan				dststart += dstinc;
3024261fa58Smacallan			}
3034261fa58Smacallan		}
3044261fa58Smacallan	}
3054261fa58Smacallan	exaMarkSync(pDstPixmap->drawable.pScreen);
3064261fa58Smacallan}
3074261fa58Smacallan
308f71acd79Smacallanstatic void
309f71acd79SmacallanCG14Copy8(PixmapPtr pDstPixmap,
310f71acd79Smacallan         int srcX, int srcY, int dstX, int dstY, int w, int h)
311f71acd79Smacallan{
312f71acd79Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
313f71acd79Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
314f71acd79Smacallan	int dstpitch, dstoff, srcpitch, srcoff;
315f71acd79Smacallan	int srcstart, dststart, xinc, srcinc, dstinc;
316f71acd79Smacallan	int line, count, s, d, num;
317f71acd79Smacallan
318f71acd79Smacallan	ENTER;
319f71acd79Smacallan	dstpitch = exaGetPixmapPitch(pDstPixmap);
320f71acd79Smacallan	dstoff = exaGetPixmapOffset(pDstPixmap);
321f71acd79Smacallan	srcpitch = p->srcpitch;
322f71acd79Smacallan	srcoff = p->srcoff;
323f71acd79Smacallan	/*
324f71acd79Smacallan	 * should clear the WO bit in SX_CONTROL_STATUS, then check if SX
325f71acd79Smacallan	 * actually wrote anything and only sync if it did
326f71acd79Smacallan	 */
327f71acd79Smacallan	srcstart = srcX + (srcpitch * srcY) + srcoff;
328f71acd79Smacallan	dststart = dstX + (dstpitch * dstY) + dstoff;
329f71acd79Smacallan
330f71acd79Smacallan	/*
331f71acd79Smacallan	 * we always copy up to 32 pixels at a time so direction doesn't
332f71acd79Smacallan	 * matter if w<=32
333f71acd79Smacallan	 */
334f71acd79Smacallan	if (w > 32) {
335f71acd79Smacallan		if (p->xdir < 0) {
336f71acd79Smacallan			srcstart += (w - 32);
337f71acd79Smacallan			dststart += (w - 32);
338f71acd79Smacallan			xinc = -32;
339f71acd79Smacallan		} else
340f71acd79Smacallan			xinc = 32;
341f71acd79Smacallan	} else
342f71acd79Smacallan		xinc = 32;
343f71acd79Smacallan	if (p->ydir < 0) {
344f71acd79Smacallan		srcstart += (h - 1) * srcpitch;
345f71acd79Smacallan		dststart += (h - 1) * dstpitch;
346f71acd79Smacallan		srcinc = -srcpitch;
347f71acd79Smacallan		dstinc = -dstpitch;
348f71acd79Smacallan	} else {
349f71acd79Smacallan		srcinc = srcpitch;
350f71acd79Smacallan		dstinc = dstpitch;
351f71acd79Smacallan	}
352f71acd79Smacallan	if (p->last_rop == 0xcc) {
353f71acd79Smacallan		/* plain old copy */
354f71acd79Smacallan		if ( xinc > 0) {
355f71acd79Smacallan			/* going left to right */
356f71acd79Smacallan			for (line = 0; line < h; line++) {
357f71acd79Smacallan				count = 0;
358f71acd79Smacallan				s = srcstart;
359f71acd79Smacallan				d = dststart;
360f71acd79Smacallan				while ( count < w) {
361f71acd79Smacallan					num = min(32, w - count);
362f71acd79Smacallan					write_sx_io(p, s,
363f71acd79Smacallan					    SX_LDB(10, num - 1, s & 7));
364f71acd79Smacallan					write_sx_io(p, d,
365f71acd79Smacallan					    SX_STBM(10, num - 1, d & 7));
366f71acd79Smacallan					s += xinc;
367f71acd79Smacallan					d += xinc;
368f71acd79Smacallan					count += 32;
369f71acd79Smacallan				}
370f71acd79Smacallan				srcstart += srcinc;
371f71acd79Smacallan				dststart += dstinc;
372f71acd79Smacallan			}
373f71acd79Smacallan		} else {
374f71acd79Smacallan			/* going right to left */
375f71acd79Smacallan			int i, chunks = (w >> 5);
376f71acd79Smacallan			for (line = 0; line < h; line++) {
377f71acd79Smacallan				s = srcstart;
378f71acd79Smacallan				d = dststart;
379f71acd79Smacallan				count = w;
380f71acd79Smacallan				for (i = 0; i < chunks; i++) {
381f71acd79Smacallan					write_sx_io(p, s,
382f71acd79Smacallan					    SX_LDB(10, 31, s & 7));
383f71acd79Smacallan					write_sx_io(p, d,
384f71acd79Smacallan					    SX_STBM(10, 31, d & 7));
385f71acd79Smacallan					s -= 32;
386f71acd79Smacallan					d -= 32;
387f71acd79Smacallan					count -= 32;
388f71acd79Smacallan				}
389f71acd79Smacallan				/* leftovers, if any */
390f71acd79Smacallan				if (count > 0) {
391f71acd79Smacallan					s += (32 - count);
392f71acd79Smacallan					d += (32 - count);
393f71acd79Smacallan					write_sx_io(p, s,
394f71acd79Smacallan					    SX_LDB(10, count - 1, s & 7));
395f71acd79Smacallan					write_sx_io(p, d,
396f71acd79Smacallan					    SX_STBM(10, count - 1, d & 7));
397f71acd79Smacallan				}
398f71acd79Smacallan				srcstart += srcinc;
399f71acd79Smacallan				dststart += dstinc;
400f71acd79Smacallan			}
401f71acd79Smacallan		}
402f71acd79Smacallan	} else {
403f71acd79Smacallan		/* ROPs needed */
404f71acd79Smacallan		if ( xinc > 0) {
405f71acd79Smacallan			/* going left to right */
406f71acd79Smacallan			for (line = 0; line < h; line++) {
407f71acd79Smacallan				count = 0;
408f71acd79Smacallan				s = srcstart;
409f71acd79Smacallan				d = dststart;
410f71acd79Smacallan				while ( count < w) {
411f71acd79Smacallan					num = min(32, w - count);
412f71acd79Smacallan					write_sx_io(p, s,
413f71acd79Smacallan					    SX_LDB(10, num - 1, s & 7));
414f71acd79Smacallan					write_sx_io(p, d,
415f71acd79Smacallan					    SX_LDB(42, num - 1, d & 7));
416f71acd79Smacallan					if (num > 16) {
417f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
418f71acd79Smacallan					    	 SX_ROP(10, 42, 74, 15));
419f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
420f71acd79Smacallan					    	 SX_ROP(26, 58, 90, num - 17));
421f71acd79Smacallan					} else {
422f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
423f71acd79Smacallan					    	 SX_ROP(10, 42, 74, num - 1));
424f71acd79Smacallan					}
425f71acd79Smacallan					write_sx_io(p, d,
426f71acd79Smacallan					    SX_STBM(74, num - 1, d & 7));
427f71acd79Smacallan					s += xinc;
428f71acd79Smacallan					d += xinc;
429f71acd79Smacallan					count += 32;
430f71acd79Smacallan				}
431f71acd79Smacallan				srcstart += srcinc;
432f71acd79Smacallan				dststart += dstinc;
433f71acd79Smacallan			}
434f71acd79Smacallan		} else {
435f71acd79Smacallan			/* going right to left */
436f71acd79Smacallan			int i, chunks = (w >> 5);
437f71acd79Smacallan			for (line = 0; line < h; line++) {
438f71acd79Smacallan				s = srcstart;
439f71acd79Smacallan				d = dststart;
440f71acd79Smacallan				count = w;
441f71acd79Smacallan				for (i = 0; i < chunks; i++) {
442f71acd79Smacallan					write_sx_io(p, s, SX_LDB(10, 31, s & 7));
443f71acd79Smacallan					write_sx_io(p, d, SX_LDB(42, 31, d & 7));
444f71acd79Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
445f71acd79Smacallan				    	    SX_ROP(10, 42, 74, 15));
446f71acd79Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
447f71acd79Smacallan				    	    SX_ROP(26, 58, 90, 15));
448f71acd79Smacallan					write_sx_io(p, d,
449f71acd79Smacallan					    SX_STBM(74, 31, d & 7));
450f71acd79Smacallan					s -= 128;
451f71acd79Smacallan					d -= 128;
452f71acd79Smacallan					count -= 32;
453f71acd79Smacallan				}
454f71acd79Smacallan				/* leftovers, if any */
455f71acd79Smacallan				if (count > 0) {
456f71acd79Smacallan					s += (32 - count);
457f71acd79Smacallan					d += (32 - count);
458f71acd79Smacallan					write_sx_io(p, s,
459f71acd79Smacallan					    SX_LDB(10, count - 1, s & 7));
460f71acd79Smacallan					write_sx_io(p, d,
461f71acd79Smacallan					    SX_LDB(42, count - 1, d & 7));
462f71acd79Smacallan					if (count > 16) {
463f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
464f71acd79Smacallan					    	    SX_ROP(10, 42, 74, 15));
465f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
466f71acd79Smacallan					    	 SX_ROP(26, 58, 90, count - 17));
467f71acd79Smacallan					} else {
468f71acd79Smacallan						write_sx_reg(p, SX_INSTRUCTIONS,
469f71acd79Smacallan					    	 SX_ROP(10, 42, 74, count - 1));
470f71acd79Smacallan					}
471f71acd79Smacallan
472f71acd79Smacallan					write_sx_io(p, d,
473f71acd79Smacallan					    SX_STBM(74, count - 1, d & 7));
474f71acd79Smacallan				}
475f71acd79Smacallan				srcstart += srcinc;
476f71acd79Smacallan				dststart += dstinc;
477f71acd79Smacallan			}
478f71acd79Smacallan		}
479f71acd79Smacallan	}
480f71acd79Smacallan	exaMarkSync(pDstPixmap->drawable.pScreen);
481f71acd79Smacallan}
482f71acd79Smacallan
4834261fa58Smacallanstatic void
4844261fa58SmacallanCG14DoneCopy(PixmapPtr pDstPixmap)
4854261fa58Smacallan{
4864261fa58Smacallan}
4874261fa58Smacallan
4884261fa58Smacallanstatic Bool
4894261fa58SmacallanCG14PrepareSolid(PixmapPtr pPixmap, int alu, Pixel planemask, Pixel fg)
4904261fa58Smacallan{
4914261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
4924261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
4934261fa58Smacallan
4944261fa58Smacallan	ENTER;
495b8ad197aSmacallan	DPRINTF(X_ERROR, "bits per pixel: %d %08x\n",
496b8ad197aSmacallan	    pPixmap->drawable.bitsPerPixel, fg);
497b8ad197aSmacallan
498dbf8597cSmacallan	/*
499dbf8597cSmacallan	 * GXset and GXclear are really just specual cases of GXcopy with
500dbf8597cSmacallan	 * fixed fill colour
501dbf8597cSmacallan	 */
502dbf8597cSmacallan	switch (alu) {
503dbf8597cSmacallan		case GXclear:
504dbf8597cSmacallan			alu = GXcopy;
505dbf8597cSmacallan			fg = 0;
506dbf8597cSmacallan			break;
507dbf8597cSmacallan		case GXset:
508dbf8597cSmacallan			alu = GXcopy;
509dbf8597cSmacallan			fg = 0xffffffff;
510dbf8597cSmacallan			break;
511dbf8597cSmacallan	}
512b8ad197aSmacallan	/* repeat the colour in every sub byte if we're in 8 bit */
513b8ad197aSmacallan	if (pPixmap->drawable.bitsPerPixel == 8) {
514b8ad197aSmacallan		fg |= fg << 8;
515b8ad197aSmacallan		fg |= fg << 16;
516b8ad197aSmacallan	}
5174261fa58Smacallan	write_sx_reg(p, SX_QUEUED(8), fg);
5184261fa58Smacallan	write_sx_reg(p, SX_QUEUED(9), fg);
5194261fa58Smacallan	if (planemask != p->last_mask) {
5204261fa58Smacallan		CG14Wait(p);
5214261fa58Smacallan		write_sx_reg(p, SX_PLANEMASK, planemask);
5224261fa58Smacallan		p->last_mask = planemask;
5234261fa58Smacallan	}
5244261fa58Smacallan	alu = sx_rop[alu];
5254261fa58Smacallan	if (alu != p->last_rop) {
5264261fa58Smacallan		CG14Wait(p);
5274261fa58Smacallan		write_sx_reg(p, SX_ROP_CONTROL, alu);
5284261fa58Smacallan		p->last_rop = alu;
5294261fa58Smacallan	}
530dbf8597cSmacallan
5314261fa58Smacallan	DPRINTF(X_ERROR, "%s: %x\n", __func__, alu);
5324261fa58Smacallan	return TRUE;
5334261fa58Smacallan}
5344261fa58Smacallan
5354261fa58Smacallanstatic void
5364261fa58SmacallanCG14Solid32(Cg14Ptr p, uint32_t start, uint32_t pitch, int w, int h)
5374261fa58Smacallan{
5384261fa58Smacallan	int line, x, num;
5394261fa58Smacallan	uint32_t ptr;
5404261fa58Smacallan
5414261fa58Smacallan	ENTER;
5424261fa58Smacallan	if (p->last_rop == 0xcc) {
5434261fa58Smacallan		/* simple fill */
5444261fa58Smacallan		for (line = 0; line < h; line++) {
5454261fa58Smacallan			x = 0;
5464261fa58Smacallan			while (x < w) {
5474261fa58Smacallan				ptr = start + (x << 2);
5484261fa58Smacallan				num = min(32, w - x);
5494261fa58Smacallan				write_sx_io(p, ptr,
5504261fa58Smacallan				    SX_STS(8, num - 1, ptr & 7));
5514261fa58Smacallan				x += 32;
5524261fa58Smacallan			}
5534261fa58Smacallan			start += pitch;
5544261fa58Smacallan		}
5554261fa58Smacallan	} else if (p->last_rop == 0xaa) {
5564261fa58Smacallan		/* nothing to do here */
5574261fa58Smacallan		return;
5584261fa58Smacallan	} else {
5594261fa58Smacallan		/* alright, let's do actual ROP stuff */
5604261fa58Smacallan
5614261fa58Smacallan		/* first repeat the fill colour into 16 registers */
5624261fa58Smacallan		write_sx_reg(p, SX_INSTRUCTIONS,
5634261fa58Smacallan		    SX_SELECT_S(8, 8, 10, 15));
5644261fa58Smacallan
5654261fa58Smacallan		for (line = 0; line < h; line++) {
5664261fa58Smacallan			x = 0;
5674261fa58Smacallan			while (x < w) {
5684261fa58Smacallan				ptr = start + (x << 2);
5694261fa58Smacallan				num = min(32, w - x);
5704261fa58Smacallan				/* now suck fb data into registers */
5714261fa58Smacallan				write_sx_io(p, ptr,
5724261fa58Smacallan				    SX_LD(42, num - 1, ptr & 7));
5734261fa58Smacallan				/*
5744261fa58Smacallan				 * ROP them with the fill data we left in 10
5754261fa58Smacallan				 * non-memory ops can only have counts up to 16
5764261fa58Smacallan				 */
5774261fa58Smacallan				if (num <= 16) {
5784261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
5794261fa58Smacallan					    SX_ROP(10, 42, 74, num - 1));
5804261fa58Smacallan				} else {
5814261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
5824261fa58Smacallan					    SX_ROP(10, 42, 74, 15));
5834261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
5844261fa58Smacallan					    SX_ROP(10, 58, 90, num - 17));
5854261fa58Smacallan				}
5864261fa58Smacallan				/* and write the result back into memory */
5874261fa58Smacallan				write_sx_io(p, ptr,
5884261fa58Smacallan				    SX_ST(74, num - 1, ptr & 7));
5894261fa58Smacallan				x += 32;
5904261fa58Smacallan			}
5914261fa58Smacallan			start += pitch;
5924261fa58Smacallan		}
5934261fa58Smacallan	}
5944261fa58Smacallan}
5954261fa58Smacallan
5964261fa58Smacallanstatic void
5974261fa58SmacallanCG14Solid8(Cg14Ptr p, uint32_t start, uint32_t pitch, int w, int h)
5984261fa58Smacallan{
599dbf8597cSmacallan	int line, num, pre, cnt;
6004261fa58Smacallan	uint32_t ptr;
6014261fa58Smacallan
6024261fa58Smacallan	ENTER;
603b8ad197aSmacallan	pre = start & 3;
604b8ad197aSmacallan	if (pre != 0) pre = 4 - pre;
6054261fa58Smacallan
6064261fa58Smacallan	if (p->last_rop == 0xcc) {
6074261fa58Smacallan		/* simple fill */
6084261fa58Smacallan		for (line = 0; line < h; line++) {
609b8ad197aSmacallan			ptr = start;
610b8ad197aSmacallan			cnt = w;
611b46cab2aSmacallan			pre = min(pre, cnt);
612b8ad197aSmacallan			if (pre) {
613b8ad197aSmacallan				write_sx_io(p, ptr & ~7, SX_STBS(8, pre - 1, ptr & 7));
614b8ad197aSmacallan				ptr += pre;
615b8ad197aSmacallan				cnt -= pre;
616b46cab2aSmacallan				if (cnt == 0) goto next;
617b8ad197aSmacallan			}
618b8ad197aSmacallan			/* now do the aligned pixels in 32bit chunks */
619b8ad197aSmacallan			if (ptr & 3) xf86Msg(X_ERROR, "%s %x\n", __func__, ptr);
620b8ad197aSmacallan			while(cnt > 3) {
621b8ad197aSmacallan				num = min(32, cnt >> 2);
622b8ad197aSmacallan				write_sx_io(p, ptr & ~7, SX_STS(8, num - 1, ptr & 7));
623b8ad197aSmacallan				ptr += num << 2;
624b8ad197aSmacallan				cnt -= num << 2;
625b8ad197aSmacallan			}
626b8ad197aSmacallan			if (cnt > 3) xf86Msg(X_ERROR, "%s cnt %d\n", __func__, cnt);
627b8ad197aSmacallan			if (cnt > 0) {
628b8ad197aSmacallan				write_sx_io(p, ptr & ~7, SX_STBS(8, cnt - 1, ptr & 7));
6294261fa58Smacallan			}
630b8ad197aSmacallan			if ((ptr + cnt) != (start + w)) xf86Msg(X_ERROR, "%s %x vs %x\n", __func__, ptr + cnt, start + w);
631b46cab2aSmacallannext:
6324261fa58Smacallan			start += pitch;
6334261fa58Smacallan		}
6344261fa58Smacallan	} else if (p->last_rop == 0xaa) {
6354261fa58Smacallan		/* nothing to do here */
6364261fa58Smacallan		return;
6374261fa58Smacallan	} else {
6384261fa58Smacallan		/* alright, let's do actual ROP stuff */
6394261fa58Smacallan
6404261fa58Smacallan		/* first repeat the fill colour into 16 registers */
6414261fa58Smacallan		write_sx_reg(p, SX_INSTRUCTIONS,
6424261fa58Smacallan		    SX_SELECT_S(8, 8, 10, 15));
6434261fa58Smacallan
6444261fa58Smacallan		for (line = 0; line < h; line++) {
645dbf8597cSmacallan			ptr = start;
646dbf8597cSmacallan			cnt = w;
647dbf8597cSmacallan			pre = min(pre, cnt);
648dbf8597cSmacallan			if (pre) {
649dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_LDB(26, pre - 1, ptr & 7));
650dbf8597cSmacallan				write_sx_reg(p, SX_INSTRUCTIONS, SX_ROP(10, 26, 42, pre - 1));
651dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_STB(42, pre - 1, ptr & 7));
652dbf8597cSmacallan				ptr += pre;
653dbf8597cSmacallan				cnt -= pre;
654dbf8597cSmacallan				if (cnt == 0) goto next2;
655dbf8597cSmacallan			}
656dbf8597cSmacallan			/* now do the aligned pixels in 32bit chunks */
657dbf8597cSmacallan			if (ptr & 3) xf86Msg(X_ERROR, "%s %x\n", __func__, ptr);
658dbf8597cSmacallan			while(cnt > 3) {
659dbf8597cSmacallan				num = min(32, cnt >> 2);
660dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_LD(26, num - 1, ptr & 7));
6614261fa58Smacallan				if (num <= 16) {
6624261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
663dbf8597cSmacallan					    SX_ROP(10, 26, 58, num - 1));
6644261fa58Smacallan				} else {
6654261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
666dbf8597cSmacallan					    SX_ROP(10, 26, 58, 15));
6674261fa58Smacallan					write_sx_reg(p, SX_INSTRUCTIONS,
668dbf8597cSmacallan					    SX_ROP(10, 42, 74, num - 17));
6694261fa58Smacallan				}
670dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_ST(58, num - 1, ptr & 7));
671dbf8597cSmacallan				ptr += num << 2;
672dbf8597cSmacallan				cnt -= num << 2;
6734261fa58Smacallan			}
674dbf8597cSmacallan			if (cnt > 3) xf86Msg(X_ERROR, "%s cnt %d\n", __func__, cnt);
675dbf8597cSmacallan			if (cnt > 0) {
676dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_LDB(26, cnt - 1, ptr & 7));
677dbf8597cSmacallan				write_sx_reg(p, SX_INSTRUCTIONS, SX_ROP(10, 26, 42, cnt - 1));
678dbf8597cSmacallan				write_sx_io(p, ptr & ~7, SX_STB(42, cnt - 1, ptr & 7));
679dbf8597cSmacallan			}
680dbf8597cSmacallan			if ((ptr + cnt) != (start + w)) xf86Msg(X_ERROR, "%s %x vs %x\n", __func__, ptr + cnt, start + w);
681dbf8597cSmacallannext2:
6824261fa58Smacallan			start += pitch;
6834261fa58Smacallan		}
6844261fa58Smacallan	}
6854261fa58Smacallan}
6864261fa58Smacallan
6874261fa58Smacallanstatic void
6884261fa58SmacallanCG14Solid(PixmapPtr pPixmap, int x1, int y1, int x2, int y2)
6894261fa58Smacallan{
6904261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
6914261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
6924261fa58Smacallan	int w = x2 - x1, h = y2 - y1, dstoff, dstpitch;
6934261fa58Smacallan	int start, depth;
6944261fa58Smacallan
6954261fa58Smacallan	ENTER;
6964261fa58Smacallan	dstpitch = exaGetPixmapPitch(pPixmap);
6974261fa58Smacallan	dstoff = exaGetPixmapOffset(pPixmap);
6984261fa58Smacallan
6994261fa58Smacallan	depth = pPixmap->drawable.bitsPerPixel;
7004261fa58Smacallan	switch (depth) {
7014261fa58Smacallan		case 32:
7024261fa58Smacallan			start = dstoff + (y1 * dstpitch) + (x1 << 2);
7034261fa58Smacallan			CG14Solid32(p, start, dstpitch, w, h);
7044261fa58Smacallan			break;
7054261fa58Smacallan		case 8:
7064261fa58Smacallan			start = dstoff + (y1 * dstpitch) + x1;
7074261fa58Smacallan			CG14Solid8(p, start, dstpitch, w, h);
7084261fa58Smacallan			break;
7094261fa58Smacallan	}
7104261fa58Smacallan
7114261fa58Smacallan	DPRINTF(X_ERROR, "Solid %d %d %d %d, %d %d -> %d\n", x1, y1, x2, y2,
7124261fa58Smacallan	    dstpitch, dstoff, start);
7134261fa58Smacallan	DPRINTF(X_ERROR, "%x %x %x\n", p->last_rop,
7144261fa58Smacallan	    read_sx_reg(p, SX_QUEUED(8)), read_sx_reg(p, SX_QUEUED(9)));
7154261fa58Smacallan	exaMarkSync(pPixmap->drawable.pScreen);
7164261fa58Smacallan}
7174261fa58Smacallan
7184261fa58Smacallan/*
7194261fa58Smacallan * Memcpy-based UTS.
7204261fa58Smacallan */
7214261fa58Smacallanstatic Bool
7224261fa58SmacallanCG14UploadToScreen(PixmapPtr pDst, int x, int y, int w, int h,
7234261fa58Smacallan    char *src, int src_pitch)
7244261fa58Smacallan{
7254261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
7264261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
7274261fa58Smacallan	char  *dst        = p->fb + exaGetPixmapOffset(pDst);
7284261fa58Smacallan	int    dst_pitch  = exaGetPixmapPitch(pDst);
7294261fa58Smacallan
7304261fa58Smacallan	int bpp    = pDst->drawable.bitsPerPixel;
7314261fa58Smacallan	int cpp    = (bpp + 7) >> 3;
7324261fa58Smacallan	int wBytes = w * cpp;
7334261fa58Smacallan
7344261fa58Smacallan	ENTER;
735f71acd79Smacallan	DPRINTF(X_ERROR, "%s depth %d\n", __func__, bpp);
7364261fa58Smacallan	dst += (x * cpp) + (y * dst_pitch);
7374261fa58Smacallan
7384261fa58Smacallan	CG14Wait(p);
7394261fa58Smacallan
7404261fa58Smacallan	while (h--) {
7414261fa58Smacallan		memcpy(dst, src, wBytes);
7424261fa58Smacallan		src += src_pitch;
7434261fa58Smacallan		dst += dst_pitch;
7444261fa58Smacallan	}
7454261fa58Smacallan	__asm("stbar;");
7464261fa58Smacallan	return TRUE;
7474261fa58Smacallan}
7484261fa58Smacallan
7494261fa58Smacallan/*
7504261fa58Smacallan * Memcpy-based DFS.
7514261fa58Smacallan */
7524261fa58Smacallanstatic Bool
7534261fa58SmacallanCG14DownloadFromScreen(PixmapPtr pSrc, int x, int y, int w, int h,
7544261fa58Smacallan    char *dst, int dst_pitch)
7554261fa58Smacallan{
7564261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pSrc->drawable.pScreen->myNum];
7574261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
7584261fa58Smacallan	char  *src        = p->fb + exaGetPixmapOffset(pSrc);
7594261fa58Smacallan	int    src_pitch  = exaGetPixmapPitch(pSrc);
7604261fa58Smacallan
7614261fa58Smacallan	ENTER;
7624261fa58Smacallan	int bpp    = pSrc->drawable.bitsPerPixel;
7634261fa58Smacallan	int cpp    = (bpp + 7) >> 3;
7644261fa58Smacallan	int wBytes = w * cpp;
7654261fa58Smacallan
7664261fa58Smacallan	src += (x * cpp) + (y * src_pitch);
7674261fa58Smacallan
7684261fa58Smacallan	CG14Wait(p);
7694261fa58Smacallan
7704261fa58Smacallan	while (h--) {
7714261fa58Smacallan		memcpy(dst, src, wBytes);
7724261fa58Smacallan		src += src_pitch;
7734261fa58Smacallan		dst += dst_pitch;
7744261fa58Smacallan	}
7754261fa58Smacallan
7764261fa58Smacallan	return TRUE;
7774261fa58Smacallan}
7784261fa58Smacallan
7794261fa58SmacallanBool
7804261fa58SmacallanCG14CheckComposite(int op, PicturePtr pSrcPicture,
7814261fa58Smacallan                           PicturePtr pMaskPicture,
7824261fa58Smacallan                           PicturePtr pDstPicture)
7834261fa58Smacallan{
7844261fa58Smacallan	int i, ok = FALSE;
7854261fa58Smacallan
7864261fa58Smacallan	ENTER;
7874261fa58Smacallan
7884261fa58Smacallan	/*
7894261fa58Smacallan	 * SX is in theory capable of accelerating pretty much all Xrender ops,
7904261fa58Smacallan	 * even coordinate transformation and gradients. Support will be added
7914261fa58Smacallan	 * over time and likely have to spill over into its own source file.
7924261fa58Smacallan	 */
7934261fa58Smacallan
794a3a2ba44Smacallan	if ((op != PictOpOver) && (op != PictOpAdd) && (op != PictOpSrc)) {
795fe97f391Smacallan		DPRINTF(X_ERROR, "%s: rejecting %d\n", __func__, op);
7964261fa58Smacallan		return FALSE;
7974261fa58Smacallan	}
7984261fa58Smacallan
7994bd47ccfSmacallan	if (pSrcPicture != NULL) {
8004bd47ccfSmacallan		i = 0;
8014bd47ccfSmacallan		while ((i < arraysize(src_formats)) && (!ok)) {
8024bd47ccfSmacallan			ok =  (pSrcPicture->format == src_formats[i]);
8034bd47ccfSmacallan			i++;
8044bd47ccfSmacallan		}
8054bd47ccfSmacallan
8064bd47ccfSmacallan		if (!ok) {
8074bd47ccfSmacallan			DPRINTF(X_ERROR, "%s: unsupported src format %x\n",
8084bd47ccfSmacallan			    __func__, pSrcPicture->format);
8094bd47ccfSmacallan			return FALSE;
8104bd47ccfSmacallan		}
8114bd47ccfSmacallan		DPRINTF(X_ERROR, "src is %x, %d\n", pSrcPicture->format, op);
8124261fa58Smacallan	}
8134261fa58Smacallan
8144bd47ccfSmacallan	if (pDstPicture != NULL) {
8154bd47ccfSmacallan		i = 0;
8164bd47ccfSmacallan		ok = FALSE;
8174bd47ccfSmacallan		while ((i < arraysize(src_formats)) && (!ok)) {
8184bd47ccfSmacallan			ok =  (pDstPicture->format == src_formats[i]);
8194bd47ccfSmacallan			i++;
8204bd47ccfSmacallan		}
8214bd47ccfSmacallan
8224bd47ccfSmacallan		if (!ok) {
8234bd47ccfSmacallan			DPRINTF(X_ERROR, "%s: unsupported dst format %x\n",
8244bd47ccfSmacallan			    __func__, pDstPicture->format);
8254bd47ccfSmacallan			return FALSE;
8264bd47ccfSmacallan		}
8274bd47ccfSmacallan		DPRINTF(X_ERROR, "dst is %x, %d\n", pDstPicture->format, op);
8284bd47ccfSmacallan	}
8294261fa58Smacallan
8304261fa58Smacallan	if (pMaskPicture != NULL) {
8314261fa58Smacallan		DPRINTF(X_ERROR, "mask is %x %d %d\n", pMaskPicture->format,
8324261fa58Smacallan		    pMaskPicture->pDrawable->width,
8334261fa58Smacallan		    pMaskPicture->pDrawable->height);
8344261fa58Smacallan	}
8354261fa58Smacallan	return TRUE;
8364261fa58Smacallan}
8374261fa58Smacallan
8384261fa58SmacallanBool
8394261fa58SmacallanCG14PrepareComposite(int op, PicturePtr pSrcPicture,
8404261fa58Smacallan                             PicturePtr pMaskPicture,
8414261fa58Smacallan                             PicturePtr pDstPicture,
8424261fa58Smacallan                             PixmapPtr  pSrc,
8434261fa58Smacallan                             PixmapPtr  pMask,
8444261fa58Smacallan                             PixmapPtr  pDst)
8454261fa58Smacallan{
8464261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
8474261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
8484261fa58Smacallan
8494261fa58Smacallan	ENTER;
8504261fa58Smacallan
851f7cb851fSmacallan	p->no_source_pixmap = FALSE;
852f7cb851fSmacallan	p->source_is_solid = FALSE;
853f7cb851fSmacallan
854a3a2ba44Smacallan	if (pSrcPicture->format == PICT_a1) {
8556bdc2ffdSmacallan		xf86Msg(X_ERROR, "src mono, dst %x, op %d\n",
8566bdc2ffdSmacallan		    pDstPicture->format, op);
857a3a2ba44Smacallan		if (pMaskPicture != NULL) {
858a3a2ba44Smacallan			xf86Msg(X_ERROR, "msk %x\n", pMaskPicture->format);
859a3a2ba44Smacallan		}
860f7cb851fSmacallan	}
8614261fa58Smacallan	if (pSrcPicture->pSourcePict != NULL) {
8624261fa58Smacallan		if (pSrcPicture->pSourcePict->type == SourcePictTypeSolidFill) {
8634261fa58Smacallan			p->fillcolour =
8644261fa58Smacallan			    pSrcPicture->pSourcePict->solidFill.color;
865f7cb851fSmacallan			DPRINTF(X_ERROR, "%s: solid src %08x\n",
8664261fa58Smacallan			    __func__, p->fillcolour);
867f7cb851fSmacallan			p->no_source_pixmap = TRUE;
868f7cb851fSmacallan			p->source_is_solid = TRUE;
8694261fa58Smacallan		}
8704261fa58Smacallan	}
8714261fa58Smacallan	if ((pMaskPicture != NULL) && (pMaskPicture->pSourcePict != NULL)) {
8724261fa58Smacallan		if (pMaskPicture->pSourcePict->type ==
8734261fa58Smacallan		    SourcePictTypeSolidFill) {
8744261fa58Smacallan			p->fillcolour =
8754261fa58Smacallan			   pMaskPicture->pSourcePict->solidFill.color;
876a3a2ba44Smacallan			xf86Msg(X_ERROR, "%s: solid mask %08x\n",
8774261fa58Smacallan			    __func__, p->fillcolour);
8784261fa58Smacallan		}
8794261fa58Smacallan	}
8804261fa58Smacallan	if (pMaskPicture != NULL) {
881239808baSmacallan		p->mskoff = exaGetPixmapOffset(pMask);
8824261fa58Smacallan		p->mskpitch = exaGetPixmapPitch(pMask);
8834261fa58Smacallan		p->mskformat = pMaskPicture->format;
884a3a2ba44Smacallan	} else {
885239808baSmacallan		p->mskoff = 0;
886a3a2ba44Smacallan		p->mskpitch = 0;
887a3a2ba44Smacallan		p->mskformat = 0;
8884261fa58Smacallan	}
889f7cb851fSmacallan	if (pSrc != NULL) {
890f7cb851fSmacallan		p->source_is_solid =
891f7cb851fSmacallan		   ((pSrc->drawable.width == 1) && (pSrc->drawable.height == 1));
892f7cb851fSmacallan		p->srcoff = exaGetPixmapOffset(pSrc);
893f7cb851fSmacallan		p->srcpitch = exaGetPixmapPitch(pSrc);
894f7cb851fSmacallan		if (p->source_is_solid) {
895f7cb851fSmacallan			p->fillcolour = *(uint32_t *)(p->fb + p->srcoff);
896f7cb851fSmacallan		}
897f7cb851fSmacallan	}
8984261fa58Smacallan	p->srcformat = pSrcPicture->format;
8994261fa58Smacallan	p->dstformat = pDstPicture->format;
900f7cb851fSmacallan
901f7cb851fSmacallan	if (p->source_is_solid) {
902f7cb851fSmacallan		uint32_t temp;
903f7cb851fSmacallan
904f7cb851fSmacallan		/* stuff source colour into SX registers, swap as needed */
905f7cb851fSmacallan		temp = p->fillcolour;
906f7cb851fSmacallan		switch (p->srcformat) {
907f7cb851fSmacallan			case PICT_a8r8g8b8:
908f7cb851fSmacallan			case PICT_x8r8g8b8:
909f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(9), temp & 0xff);
910f7cb851fSmacallan				temp = temp >> 8;
911f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(10), temp & 0xff);
912f7cb851fSmacallan				temp = temp >> 8;
913f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(11), temp & 0xff);
914f7cb851fSmacallan				break;
915f7cb851fSmacallan			case PICT_a8b8g8r8:
916f7cb851fSmacallan			case PICT_x8b8g8r8:
917f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(11), temp & 0xff);
918f7cb851fSmacallan				temp = temp >> 8;
919f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(10), temp & 0xff);
920f7cb851fSmacallan				temp = temp >> 8;
921f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(9), temp & 0xff);
922f7cb851fSmacallan				break;
923f7cb851fSmacallan		}
924f7cb851fSmacallan		write_sx_reg(p, SX_QUEUED(8), 0xff);
925f7cb851fSmacallan	}
9264261fa58Smacallan	p->op = op;
927a3a2ba44Smacallan	if (op == PictOpSrc) {
928a3a2ba44Smacallan		CG14PrepareCopy(pSrc, pDst, 1, 1, GXcopy, 0xffffffff);
929a3a2ba44Smacallan	}
9304261fa58Smacallan#ifdef SX_DEBUG
9314261fa58Smacallan	DPRINTF(X_ERROR, "%x %x -> %x\n", p->srcoff, p->mskoff,
9324261fa58Smacallan	    *(uint32_t *)(p->fb + p->srcoff));
9334261fa58Smacallan#endif
9344261fa58Smacallan	return TRUE;
9354261fa58Smacallan}
9364261fa58Smacallan
9374261fa58Smacallanvoid
9384261fa58SmacallanCG14Composite(PixmapPtr pDst, int srcX, int srcY,
9394261fa58Smacallan                              int maskX, int maskY,
9404261fa58Smacallan                              int dstX, int dstY,
9414261fa58Smacallan                              int width, int height)
9424261fa58Smacallan{
9434261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
9444261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
9454261fa58Smacallan	uint32_t dstoff, dstpitch;
9464261fa58Smacallan	uint32_t dst, msk, src;
947e311bbeeSmacallan	int flip = 0;
9484261fa58Smacallan
9494261fa58Smacallan	ENTER;
9504261fa58Smacallan	dstoff = exaGetPixmapOffset(pDst);
9514261fa58Smacallan	dstpitch = exaGetPixmapPitch(pDst);
9524261fa58Smacallan
953e311bbeeSmacallan	flip = (PICT_FORMAT_TYPE(p->srcformat) !=
954e311bbeeSmacallan		PICT_FORMAT_TYPE(p->dstformat));
955e311bbeeSmacallan
9564261fa58Smacallan	switch (p->op) {
9574261fa58Smacallan		case PictOpOver:
9584261fa58Smacallan			dst = dstoff + (dstY * dstpitch) + (dstX << 2);
9594261fa58Smacallan			DPRINTF(X_ERROR, "Over %08x %08x, %d %d\n",
9604261fa58Smacallan			    p->mskformat, p->dstformat, srcX, srcY);
961a3a2ba44Smacallan			if (p->source_is_solid) {
962a3a2ba44Smacallan				switch (p->mskformat) {
963a3a2ba44Smacallan					case PICT_a8:
964a3a2ba44Smacallan						msk = p->mskoff +
965a3a2ba44Smacallan						    (maskY * p->mskpitch) +
966a3a2ba44Smacallan						    maskX;
967a3a2ba44Smacallan						CG14Comp_Over8Solid(p,
968a3a2ba44Smacallan						    msk, p->mskpitch,
969a3a2ba44Smacallan						    dst, dstpitch,
970a3a2ba44Smacallan						    width, height);
971a3a2ba44Smacallan						break;
972a3a2ba44Smacallan					case PICT_a8r8g8b8:
973a3a2ba44Smacallan					case PICT_a8b8g8r8:
974a3a2ba44Smacallan						msk = p->mskoff +
975a3a2ba44Smacallan						    (maskY * p->mskpitch) +
976a3a2ba44Smacallan						    (maskX << 2);
977a3a2ba44Smacallan						CG14Comp_Over32Solid(p,
978a3a2ba44Smacallan						    msk, p->mskpitch,
979a3a2ba44Smacallan						    dst, dstpitch,
980a3a2ba44Smacallan						    width, height);
981a3a2ba44Smacallan						break;
982a3a2ba44Smacallan					default:
983a3a2ba44Smacallan						xf86Msg(X_ERROR,
984f71acd79Smacallan						  "unsupported mask format %08x\n", p->mskformat);
985a3a2ba44Smacallan				}
986a3a2ba44Smacallan			} else {
9876bdc2ffdSmacallan				DPRINTF(X_ERROR, "non-solid over with msk %x\n",
9886bdc2ffdSmacallan				    p->mskformat);
989a3a2ba44Smacallan				switch (p->srcformat) {
990a3a2ba44Smacallan					case PICT_a8r8g8b8:
991a3a2ba44Smacallan					case PICT_a8b8g8r8:
992a3a2ba44Smacallan						src = p->srcoff +
993a3a2ba44Smacallan						    (srcY * p->srcpitch) +
994a3a2ba44Smacallan						    (srcX << 2);
995a3a2ba44Smacallan						dst = dstoff +
996a3a2ba44Smacallan						    (dstY * dstpitch) +
997a3a2ba44Smacallan						    (dstX << 2);
998a3a2ba44Smacallan						if (p->mskformat == PICT_a8) {
999a3a2ba44Smacallan							msk = p->mskoff +
1000a3a2ba44Smacallan							    (maskY * p->mskpitch) +
1001a3a2ba44Smacallan							    maskX;
1002a3a2ba44Smacallan							CG14Comp_Over32Mask(p,
1003a3a2ba44Smacallan							    src, p->srcpitch,
1004a3a2ba44Smacallan							    msk, p->mskpitch,
1005a3a2ba44Smacallan							    dst, dstpitch,
1006e311bbeeSmacallan							    width, height, flip);
1007a3a2ba44Smacallan						} else {
1008a3a2ba44Smacallan							CG14Comp_Over32(p,
1009a3a2ba44Smacallan							    src, p->srcpitch,
1010a3a2ba44Smacallan							    dst, dstpitch,
1011e311bbeeSmacallan							    width, height, flip);
1012a3a2ba44Smacallan						}
1013a3a2ba44Smacallan						break;
1014a3a2ba44Smacallan					case PICT_x8r8g8b8:
1015a3a2ba44Smacallan					case PICT_x8b8g8r8:
10166bdc2ffdSmacallan						src = p->srcoff +
10176bdc2ffdSmacallan						    (srcY * p->srcpitch) +
10186bdc2ffdSmacallan						    (srcX << 2);
10196bdc2ffdSmacallan						dst = dstoff +
10206bdc2ffdSmacallan						    (dstY * dstpitch) +
10216bdc2ffdSmacallan						    (dstX << 2);
10226bdc2ffdSmacallan						if (p->mskformat == PICT_a8) {
10236bdc2ffdSmacallan							msk = p->mskoff +
10246bdc2ffdSmacallan							    (maskY * p->mskpitch) +
10256bdc2ffdSmacallan							    maskX;
10266bdc2ffdSmacallan							CG14Comp_Over32Mask_noalpha(p,
10276bdc2ffdSmacallan							    src, p->srcpitch,
10286bdc2ffdSmacallan							    msk, p->mskpitch,
1029fa158432Smacallan							    dst, dstpitch,
1030e311bbeeSmacallan							    width, height, flip);
1031fa158432Smacallan						} else if ((p->mskformat == PICT_a8r8g8b8) ||
1032fa158432Smacallan							   (p->mskformat == PICT_a8b8g8r8)) {
1033fa158432Smacallan							msk = p->mskoff +
1034fa158432Smacallan							    (maskY * p->mskpitch) +
1035fa158432Smacallan							    (maskX << 2);
1036fa158432Smacallan							CG14Comp_Over32Mask32_noalpha(p,
1037fa158432Smacallan							    src, p->srcpitch,
1038fa158432Smacallan							    msk, p->mskpitch,
10396bdc2ffdSmacallan							    dst, dstpitch,
1040e311bbeeSmacallan							    width, height, flip);
10416bdc2ffdSmacallan						} else {
10426bdc2ffdSmacallan							xf86Msg(X_ERROR, "no src alpha, mask is %x\n", p->mskformat);
10436bdc2ffdSmacallan						}
1044a3a2ba44Smacallan						break;
1045a3a2ba44Smacallan					default:
1046a3a2ba44Smacallan						xf86Msg(X_ERROR, "%s: format %x in non-solid Over op\n",
1047a3a2ba44Smacallan						    __func__, p->srcformat);
1048a3a2ba44Smacallan				}
1049a3a2ba44Smacallan			}
10504261fa58Smacallan			break;
10514261fa58Smacallan		case PictOpAdd:
10524261fa58Smacallan			DPRINTF(X_ERROR, "Add %08x %08x\n",
10534261fa58Smacallan			    p->srcformat, p->dstformat);
10544261fa58Smacallan			switch (p->srcformat) {
10554261fa58Smacallan				case PICT_a8:
10564261fa58Smacallan					src = p->srcoff +
10574261fa58Smacallan					    (srcY * p->srcpitch) + srcX;
1058d71cb32dSmacallan					if (p->dstformat == PICT_a8) {
1059d71cb32dSmacallan						dst = dstoff +
1060d71cb32dSmacallan						      (dstY * dstpitch) + dstX;
1061d71cb32dSmacallan						CG14Comp_Add8(p,
1062d71cb32dSmacallan						    src, p->srcpitch,
1063d71cb32dSmacallan						    dst, dstpitch,
1064d71cb32dSmacallan						    width, height);
1065d71cb32dSmacallan					} else {
1066d71cb32dSmacallan						dst = dstoff +
1067d71cb32dSmacallan						      (dstY * dstpitch) +
1068d71cb32dSmacallan						      (dstX << 2);
1069d71cb32dSmacallan						CG14Comp_Add8_32(p,
1070d71cb32dSmacallan						    src, p->srcpitch,
1071d71cb32dSmacallan						    dst, dstpitch,
1072d71cb32dSmacallan						    width, height);
1073d71cb32dSmacallan					}
10744261fa58Smacallan					break;
10754261fa58Smacallan				case PICT_a8r8g8b8:
10764261fa58Smacallan				case PICT_x8r8g8b8:
10774261fa58Smacallan					src = p->srcoff +
10784261fa58Smacallan					    (srcY * p->srcpitch) + (srcX << 2);
10794261fa58Smacallan					dst = dstoff + (dstY * dstpitch) +
10804261fa58Smacallan					    (dstX << 2);
10814261fa58Smacallan					CG14Comp_Add32(p, src, p->srcpitch,
10824261fa58Smacallan					    dst, dstpitch, width, height);
10834261fa58Smacallan					break;
10844261fa58Smacallan				default:
10854261fa58Smacallan					xf86Msg(X_ERROR,
10864261fa58Smacallan					    "unsupported src format\n");
10874261fa58Smacallan			}
10884261fa58Smacallan			break;
1089a3a2ba44Smacallan		case PictOpSrc:
1090a3a2ba44Smacallan			DPRINTF(X_ERROR, "Src %08x %08x\n",
1091a3a2ba44Smacallan			    p->srcformat, p->dstformat);
1092239808baSmacallan			if (p->mskformat != 0)
1093239808baSmacallan				xf86Msg(X_ERROR, "Src mask %08x\n", p->mskformat);
1094f71acd79Smacallan			if (p->srcformat == PICT_a8) {
1095f71acd79Smacallan				CG14Copy8(pDst, srcX, srcY, dstX, dstY, width, height);
1096f71acd79Smacallan			} else {
1097f71acd79Smacallan				/* convert between RGB and BGR? */
1098f71acd79Smacallan				CG14Copy32(pDst, srcX, srcY, dstX, dstY, width, height);
1099f71acd79Smacallan			}
1100a3a2ba44Smacallan			break;
11014261fa58Smacallan		default:
11024261fa58Smacallan			xf86Msg(X_ERROR, "unsupported op %d\n", p->op);
11034261fa58Smacallan	}
11044261fa58Smacallan	exaMarkSync(pDst->drawable.pScreen);
11054261fa58Smacallan}
11064261fa58Smacallan
11074261fa58Smacallan
11084261fa58Smacallan
11094261fa58SmacallanBool
11104261fa58SmacallanCG14InitAccel(ScreenPtr pScreen)
11114261fa58Smacallan{
11124261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
11134261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
11144261fa58Smacallan	ExaDriverPtr pExa;
11154261fa58Smacallan
11164261fa58Smacallan	pExa = exaDriverAlloc();
11174261fa58Smacallan	if (!pExa)
11184261fa58Smacallan		return FALSE;
11194261fa58Smacallan
11204261fa58Smacallan	p->pExa = pExa;
11214261fa58Smacallan
11224261fa58Smacallan	pExa->exa_major = EXA_VERSION_MAJOR;
11234261fa58Smacallan	pExa->exa_minor = EXA_VERSION_MINOR;
11244261fa58Smacallan
11254261fa58Smacallan	pExa->memoryBase = p->fb;
11264261fa58Smacallan	pExa->memorySize = p->memsize;
1127b8ad197aSmacallan	pExa->offScreenBase = p->width * p->height * (pScrn->depth >> 3);
11284261fa58Smacallan
11294261fa58Smacallan	/*
11304261fa58Smacallan	 * SX memory instructions are written to 64bit aligned addresses with
11314261fa58Smacallan	 * a 3 bit displacement. Make sure the displacement remains constant
11324261fa58Smacallan	 * within one column
11334261fa58Smacallan	 */
11344261fa58Smacallan
11354261fa58Smacallan	pExa->pixmapOffsetAlign = 8;
11364261fa58Smacallan	pExa->pixmapPitchAlign = 8;
11374261fa58Smacallan
1138fe97f391Smacallan	pExa->flags = EXA_OFFSCREEN_PIXMAPS
1139f71acd79Smacallan		      | EXA_SUPPORTS_OFFSCREEN_OVERLAPS
1140f71acd79Smacallan		      /*| EXA_MIXED_PIXMAPS*/;
11414261fa58Smacallan
11424261fa58Smacallan	/*
11434261fa58Smacallan	 * these limits are bogus
11444261fa58Smacallan	 * SX doesn't deal with coordinates at all, so there is no limit but
11454261fa58Smacallan	 * we have to put something here
11464261fa58Smacallan	 */
11474261fa58Smacallan	pExa->maxX = 4096;
11484261fa58Smacallan	pExa->maxY = 4096;
11494261fa58Smacallan
11504261fa58Smacallan	pExa->WaitMarker = CG14WaitMarker;
11514261fa58Smacallan
11524261fa58Smacallan	pExa->PrepareSolid = CG14PrepareSolid;
11534261fa58Smacallan	pExa->Solid = CG14Solid;
11544261fa58Smacallan	pExa->DoneSolid = CG14DoneCopy;
11554261fa58Smacallan	pExa->PrepareCopy = CG14PrepareCopy;
1156f71acd79Smacallan	pExa->Copy = CG14Copy32;
11574261fa58Smacallan	pExa->DoneCopy = CG14DoneCopy;
11584261fa58Smacallan	if (p->use_xrender) {
11594261fa58Smacallan		pExa->CheckComposite = CG14CheckComposite;
11604261fa58Smacallan		pExa->PrepareComposite = CG14PrepareComposite;
11614261fa58Smacallan		pExa->Composite = CG14Composite;
11624261fa58Smacallan		pExa->DoneComposite = CG14DoneCopy;
11634261fa58Smacallan	}
11644261fa58Smacallan
11654261fa58Smacallan	/* EXA hits more optimized paths when it does not have to fallback
11664261fa58Smacallan	 * because of missing UTS/DFS, hook memcpy-based UTS/DFS.
11674261fa58Smacallan	 */
11684261fa58Smacallan	pExa->UploadToScreen = CG14UploadToScreen;
11694261fa58Smacallan	pExa->DownloadFromScreen = CG14DownloadFromScreen;
11704261fa58Smacallan
1171c2193d98Smacallan	p->queuecount = 0;
11724261fa58Smacallan	/* do some hardware init */
11734261fa58Smacallan	write_sx_reg(p, SX_PLANEMASK, 0xffffffff);
11744261fa58Smacallan	p->last_mask = 0xffffffff;
11754261fa58Smacallan	write_sx_reg(p, SX_ROP_CONTROL, 0xcc);
11764261fa58Smacallan	p->last_rop = 0xcc;
11774261fa58Smacallan	return exaDriverInit(pScreen, pExa);
11784261fa58Smacallan}
1179