cg14_accel.c revision 72fd264f
172fd264fSmacallan/* $NetBSD: cg14_accel.c,v 1.27 2021/12/24 04:41:40 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;
1018c65af2dSmacallan	DPRINTF(X_ERROR, "%s bpp %d rop %x\n", __func__,
10281c68cf8Smacallan	    pSrcPixmap->drawable.bitsPerPixel, alu);
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);
18772fd264fSmacallan					sxm(SX_LD, s, 10, num - 1);
18872fd264fSmacallan					sxm(SX_STM, d, 10, num - 1);
1894261fa58Smacallan					s += xinc;
1904261fa58Smacallan					d += xinc;
1914261fa58Smacallan					count += 32;
1924261fa58Smacallan				}
1934261fa58Smacallan				srcstart += srcinc;
1944261fa58Smacallan				dststart += dstinc;
1954261fa58Smacallan			}
1964261fa58Smacallan		} else {
1974261fa58Smacallan			/* going right to left */
1984261fa58Smacallan			int i, chunks = (w >> 5);
1994261fa58Smacallan			for (line = 0; line < h; line++) {
2004261fa58Smacallan				s = srcstart;
2014261fa58Smacallan				d = dststart;
2024261fa58Smacallan				count = w;
2034261fa58Smacallan				for (i = 0; i < chunks; i++) {
20472fd264fSmacallan					sxm(SX_LD, s, 10, 31);
20572fd264fSmacallan					sxm(SX_STM, d, 10, 31);
2064261fa58Smacallan					s -= 128;
2074261fa58Smacallan					d -= 128;
2084261fa58Smacallan					count -= 32;
2094261fa58Smacallan				}
2104261fa58Smacallan				/* leftovers, if any */
2114261fa58Smacallan				if (count > 0) {
2124261fa58Smacallan					s += (32 - count) << 2;
2134261fa58Smacallan					d += (32 - count) << 2;
21472fd264fSmacallan					sxm(SX_LD, s, 10, count - 1);
21572fd264fSmacallan					sxm(SX_STM, d, 10, count - 1);
2164261fa58Smacallan				}
2174261fa58Smacallan				srcstart += srcinc;
2184261fa58Smacallan				dststart += dstinc;
2194261fa58Smacallan			}
2204261fa58Smacallan		}
2214261fa58Smacallan	} else {
2224261fa58Smacallan		/* ROPs needed */
2234261fa58Smacallan		if ( xinc > 0) {
2244261fa58Smacallan			/* going left to right */
2254261fa58Smacallan			for (line = 0; line < h; line++) {
2264261fa58Smacallan				count = 0;
2274261fa58Smacallan				s = srcstart;
2284261fa58Smacallan				d = dststart;
2294261fa58Smacallan				while ( count < w) {
2304261fa58Smacallan					num = min(32, w - count);
23172fd264fSmacallan					sxm(SX_LD, s, 10, num - 1);
23272fd264fSmacallan					sxm(SX_LD, d, 42, num - 1);
2334261fa58Smacallan					if (num > 16) {
23472fd264fSmacallan						sxi(SX_ROP(10, 42, 74, 15));
23572fd264fSmacallan						sxi(SX_ROP(26, 58, 90, num - 17));
2364261fa58Smacallan					} else {
23772fd264fSmacallan						sxi(SX_ROP(10, 42, 74, num - 1));
2384261fa58Smacallan					}
23972fd264fSmacallan					sxm(SX_STM, d, 74, num - 1);
2404261fa58Smacallan					s += xinc;
2414261fa58Smacallan					d += xinc;
2424261fa58Smacallan					count += 32;
2434261fa58Smacallan				}
2444261fa58Smacallan				srcstart += srcinc;
2454261fa58Smacallan				dststart += dstinc;
2464261fa58Smacallan			}
2474261fa58Smacallan		} else {
2484261fa58Smacallan			/* going right to left */
2494261fa58Smacallan			int i, chunks = (w >> 5);
2504261fa58Smacallan			for (line = 0; line < h; line++) {
2514261fa58Smacallan				s = srcstart;
2524261fa58Smacallan				d = dststart;
2534261fa58Smacallan				count = w;
2544261fa58Smacallan				for (i = 0; i < chunks; i++) {
25572fd264fSmacallan					sxm(SX_LD, s, 10, 31);
25672fd264fSmacallan					sxm(SX_LD, d, 42, 31);
25772fd264fSmacallan					sxi(SX_ROP(10, 42, 74, 15));
25872fd264fSmacallan					sxi(SX_ROP(26, 58, 90, 15));
25972fd264fSmacallan					sxm(SX_STM, d, 74, 31);
2604261fa58Smacallan					s -= 128;
2614261fa58Smacallan					d -= 128;
2624261fa58Smacallan					count -= 32;
2634261fa58Smacallan				}
2644261fa58Smacallan				/* leftovers, if any */
2654261fa58Smacallan				if (count > 0) {
2664261fa58Smacallan					s += (32 - count) << 2;
2674261fa58Smacallan					d += (32 - count) << 2;
26872fd264fSmacallan					sxm(SX_LD, s, 10, count - 1);
26972fd264fSmacallan					sxm(SX_LD, d, 42, count - 1);
2704261fa58Smacallan					if (count > 16) {
27172fd264fSmacallan						sxi(SX_ROP(10, 42, 74, 15));
27272fd264fSmacallan						sxi(SX_ROP(26, 58, 90, count - 17));
2734261fa58Smacallan					} else {
27472fd264fSmacallan						sxi(SX_ROP(10, 42, 74, count - 1));
2754261fa58Smacallan					}
27672fd264fSmacallan					sxm(SX_STM, d, 74, count - 1);
2774261fa58Smacallan				}
2784261fa58Smacallan				srcstart += srcinc;
2794261fa58Smacallan				dststart += dstinc;
2804261fa58Smacallan			}
2814261fa58Smacallan		}
2824261fa58Smacallan	}
2834261fa58Smacallan	exaMarkSync(pDstPixmap->drawable.pScreen);
2844261fa58Smacallan}
2854261fa58Smacallan
28681c68cf8Smacallan/*
28781c68cf8Smacallan * copy with same alignment, left to right, no ROP
28881c68cf8Smacallan */
28981c68cf8Smacallanstatic void
29072fd264fSmacallanCG14Copy8_aligned_norop(Cg14Ptr p, int srcstart, int dststart, int w, int h,
29172fd264fSmacallan    int srcpitch, int dstpitch)
29281c68cf8Smacallan{
29381c68cf8Smacallan	int saddr, daddr, pre, cnt, wrds;
29481c68cf8Smacallan
29581c68cf8Smacallan	ENTER;
29681c68cf8Smacallan
29781c68cf8Smacallan	pre = srcstart & 3;
29881c68cf8Smacallan	if (pre != 0) pre = 4 - pre;
29981c68cf8Smacallan	pre = min(pre, w);
30081c68cf8Smacallan
30181c68cf8Smacallan	while (h > 0) {
30281c68cf8Smacallan		saddr = srcstart;
30381c68cf8Smacallan		daddr = dststart;
30481c68cf8Smacallan		cnt = w;
30581c68cf8Smacallan		if (pre > 0) {
30672fd264fSmacallan			sxm(SX_LDB, saddr, 8, pre - 1);
30772fd264fSmacallan			sxm(SX_STB, daddr, 8, pre - 1);
30881c68cf8Smacallan			saddr += pre;
30981c68cf8Smacallan			daddr += pre;
31081c68cf8Smacallan			cnt -= pre;
31181c68cf8Smacallan			if (cnt == 0) goto next;
31281c68cf8Smacallan		}
31381c68cf8Smacallan		while (cnt > 3) {
31481c68cf8Smacallan			wrds = min(32, cnt >> 2);
31572fd264fSmacallan			sxm(SX_LD, saddr, 8, wrds - 1);
31672fd264fSmacallan			sxm(SX_ST, daddr, 8, wrds - 1);
31781c68cf8Smacallan			saddr += wrds << 2;
31881c68cf8Smacallan			daddr += wrds << 2;
31981c68cf8Smacallan			cnt -= wrds << 2;
32081c68cf8Smacallan		}
32181c68cf8Smacallan		if (cnt > 0) {
32272fd264fSmacallan			sxm(SX_LDB, saddr, 8, cnt - 1);
32372fd264fSmacallan			sxm(SX_STB, daddr, 8, cnt - 1);
32481c68cf8Smacallan		}
32581c68cf8Smacallannext:
32681c68cf8Smacallan		srcstart += srcpitch;
32781c68cf8Smacallan		dststart += dstpitch;
32881c68cf8Smacallan		h--;
32981c68cf8Smacallan	}
33081c68cf8Smacallan}
33181c68cf8Smacallan
33281c68cf8Smacallan/*
33381c68cf8Smacallan * copy with same alignment, left to right, ROP
33481c68cf8Smacallan */
33581c68cf8Smacallanstatic void
33672fd264fSmacallanCG14Copy8_aligned_rop(Cg14Ptr p, int srcstart, int dststart, int w, int h,
33772fd264fSmacallan    int srcpitch, int dstpitch)
33881c68cf8Smacallan{
33981c68cf8Smacallan	int saddr, daddr, pre, cnt, wrds;
34081c68cf8Smacallan
34181c68cf8Smacallan	ENTER;
34281c68cf8Smacallan
34381c68cf8Smacallan	pre = srcstart & 3;
34481c68cf8Smacallan	if (pre != 0) pre = 4 - pre;
34581c68cf8Smacallan	pre = min(pre, w);
34681c68cf8Smacallan
34781c68cf8Smacallan	while (h > 0) {
34881c68cf8Smacallan		saddr = srcstart;
34981c68cf8Smacallan		daddr = dststart;
35081c68cf8Smacallan		cnt = w;
35181c68cf8Smacallan		if (pre > 0) {
35272fd264fSmacallan			sxm(SX_LDB, saddr, 8, pre - 1);
35372fd264fSmacallan			sxm(SX_LDB, daddr, 40, pre - 1);
35472fd264fSmacallan			sxi(SX_ROP(8, 40, 72, pre - 1));
35572fd264fSmacallan			sxm(SX_STB, daddr, 72, pre - 1);
35681c68cf8Smacallan			saddr += pre;
35781c68cf8Smacallan			daddr += pre;
35881c68cf8Smacallan			cnt -= pre;
35981c68cf8Smacallan			if (cnt == 0) goto next;
36081c68cf8Smacallan		}
36181c68cf8Smacallan		while (cnt > 3) {
36281c68cf8Smacallan			wrds = min(32, cnt >> 2);
36372fd264fSmacallan			sxm(SX_LD, saddr, 8, wrds - 1);
36472fd264fSmacallan			sxm(SX_LD, daddr, 40, wrds - 1);
36581c68cf8Smacallan			if (cnt > 16) {
36672fd264fSmacallan				sxi(SX_ROP(8, 40, 72, 15));
36772fd264fSmacallan				sxi(SX_ROP(8, 56, 88, wrds - 17));
36881c68cf8Smacallan			} else
36972fd264fSmacallan				sxi(SX_ROP(8, 40, 72, wrds - 1));
37072fd264fSmacallan			sxm(SX_ST, daddr, 72, wrds - 1);
37181c68cf8Smacallan			saddr += wrds << 2;
37281c68cf8Smacallan			daddr += wrds << 2;
37381c68cf8Smacallan			cnt -= wrds << 2;
37481c68cf8Smacallan		}
37581c68cf8Smacallan		if (cnt > 0) {
37672fd264fSmacallan			sxm(SX_LDB, saddr, 8, cnt - 1);
37772fd264fSmacallan			sxm(SX_LDB, daddr, 40, cnt - 1);
37872fd264fSmacallan			sxi(SX_ROP(8, 40, 72, cnt - 1));
37972fd264fSmacallan			sxm(SX_STB, daddr, 72, cnt - 1);
38081c68cf8Smacallan		}
38181c68cf8Smacallannext:
38281c68cf8Smacallan		srcstart += srcpitch;
38381c68cf8Smacallan		dststart += dstpitch;
38481c68cf8Smacallan		h--;
38581c68cf8Smacallan	}
38681c68cf8Smacallan}
38781c68cf8Smacallan
388f787bc61Smacallan/* up to 124 pixels so direction doesn't matter, unaligned, ROP */
389f787bc61Smacallanstatic void
390f787bc61SmacallanCG14Copy8_short_rop(Cg14Ptr p, int srcstart, int dststart, int w, int h, int srcpitch, int dstpitch)
391f787bc61Smacallan{
392f787bc61Smacallan	int saddr, daddr, pre, dist, wrds, swrds, spre, sreg, restaddr, post;
3939d7fb28bSmacallan	int ssreg;
394f787bc61Smacallan#ifdef DEBUG
395f787bc61Smacallan	int taddr = 4 + dstpitch * 50;
396f787bc61Smacallan#endif
397f787bc61Smacallan	uint32_t lmask, rmask;
398f787bc61Smacallan	ENTER;
399f787bc61Smacallan
400f787bc61Smacallan	pre = dststart & 3;
401f787bc61Smacallan	lmask = 0xffffffff >> pre;
402f787bc61Smacallan	spre = srcstart & 3;
403f787bc61Smacallan	/*
404f787bc61Smacallan	 * make sure we count all the words needed to cover the destination
405f787bc61Smacallan	 * line, covering potential partials on both ends
406f787bc61Smacallan	 */
407f787bc61Smacallan	wrds = (w + pre + 3) >> 2;
408f787bc61Smacallan	swrds = (w + spre + 3) >> 2;
409f787bc61Smacallan
410f787bc61Smacallan	if (spre < pre) {
411f787bc61Smacallan		dist = 32 - (pre - spre) * 8;
412f787bc61Smacallan		sreg = 9;
413f787bc61Smacallan	} else {
414f787bc61Smacallan		dist = (spre - pre) * 8;
415f787bc61Smacallan		sreg = 8;
416f787bc61Smacallan	}
417f787bc61Smacallan
418f787bc61Smacallan	/*
419f787bc61Smacallan	 * mask out trailing pixels to avoid partial writes
420f787bc61Smacallan	 */
421f787bc61Smacallan	post = (dststart + w) & 3;
42276a85281Smacallan	if (post != 0) {
42376a85281Smacallan		rmask = ~(0xffffffff >> (post * 8));
42476a85281Smacallan		write_sx_reg(p, SX_QUEUED(7), rmask);
42576a85281Smacallan		write_sx_reg(p, SX_QUEUED(6), ~rmask);
42676a85281Smacallan	}
42776a85281Smacallan
428f787bc61Smacallan	DPRINTF(X_ERROR, "%s %d %d, %d %d %08x %d %d %d %d %08x\n", __func__,
429f787bc61Smacallan	    w, h, spre, pre, lmask, dist, sreg, wrds, post, rmask);
430f787bc61Smacallan
431f787bc61Smacallan	/* mask out the leading pixels in dst by using a mask and ROP */
43276a85281Smacallan	if (pre != 0) {
433c1537409Smacallan		CG14Wait(p);
43476a85281Smacallan		write_sx_reg(p, SX_ROP_CONTROL, (p->last_rop & 0xf0) | 0xa);
43576a85281Smacallan		write_sx_reg(p, SX_QUEUED(R_MASK), 0xffffffff);
43676a85281Smacallan	}
437f787bc61Smacallan
438f787bc61Smacallan	saddr = srcstart & ~3;
439f787bc61Smacallan	daddr = dststart & ~3;
44076a85281Smacallan
441f787bc61Smacallan	while (h > 0) {
44272fd264fSmacallan		sxm(SX_LD, daddr, 80, wrds - 1);
44372fd264fSmacallan		sxm(SX_LD, saddr, sreg, swrds - 1);
444f787bc61Smacallan		if (wrds > 15) {
4459d7fb28bSmacallan			if (dist != 0) {
44672fd264fSmacallan				sxi(SX_FUNNEL_I(8, dist, 40, 15));
44772fd264fSmacallan				sxi(SX_FUNNEL_I(24, dist, 56, wrds - 16));
4489d7fb28bSmacallan				/* shifted source pixels are now at register 40+ */
4499d7fb28bSmacallan				ssreg = 40;
4509d7fb28bSmacallan			} else ssreg = 8;
451f787bc61Smacallan			if (pre != 0) {
452f787bc61Smacallan				/* mask out leading junk */
453f787bc61Smacallan				write_sx_reg(p, SX_QUEUED(R_MASK), lmask);
45472fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, 8, 0));
455f787bc61Smacallan				write_sx_reg(p, SX_QUEUED(R_MASK), 0xffffffff);
45672fd264fSmacallan				sxi(SX_ROPB(ssreg + 1, 81, 9, 14));
457f787bc61Smacallan			} else {
45872fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, 8, 15));
459f787bc61Smacallan			}
46072fd264fSmacallan			sxi(SX_ROPB(ssreg + 16, 96, 24, wrds - 16));
461f787bc61Smacallan		} else {
4629d7fb28bSmacallan			if (dist != 0) {
46372fd264fSmacallan				sxi(SX_FUNNEL_I(8, dist, 40, wrds));
4649d7fb28bSmacallan				ssreg = 40;
4659d7fb28bSmacallan			} else ssreg = 8;
466f787bc61Smacallan			if (pre != 0) {
467f787bc61Smacallan				/* mask out leading junk */
468f787bc61Smacallan				write_sx_reg(p, SX_QUEUED(R_MASK), lmask);
46972fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, 8, 0));
470f787bc61Smacallan				write_sx_reg(p, SX_QUEUED(R_MASK), 0xffffffff);
47172fd264fSmacallan				sxi(SX_ROPB(ssreg + 1, 81, 9, wrds));
472f787bc61Smacallan			} else {
47372fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, 8, wrds));
474f787bc61Smacallan			}
475f787bc61Smacallan		}
476f787bc61Smacallan		if (post != 0) {
477f787bc61Smacallan			/*
478f787bc61Smacallan			 * if the last word to be written out is a partial we
479f787bc61Smacallan			 * mask out the leftovers and replace them with
480f787bc61Smacallan			 * background pixels
481f787bc61Smacallan			 * we could pull the same ROP * mask trick as we do on
482f787bc61Smacallan			 * the left end but it's less annoying this way and
483f787bc61Smacallan			 * the instruction count is the same
484f787bc61Smacallan			 */
48572fd264fSmacallan			sxi(SX_ANDS(7 + wrds, 7, 5, 0));
48672fd264fSmacallan			sxi(SX_ANDS(79 + wrds, 6, 4, 0));
48772fd264fSmacallan			sxi(SX_ORS(5, 4, 7 + wrds, 0));
488f787bc61Smacallan		}
489f787bc61Smacallan#ifdef DEBUG
49072fd264fSmacallan		sxm(SX_ST, taddr, 40, wrds - 1);
491f787bc61Smacallan		taddr += dstpitch;
492f787bc61Smacallan#endif
49372fd264fSmacallan		sxm(SX_ST, daddr, 8, wrds - 1);
494f787bc61Smacallan		saddr += srcpitch;
495f787bc61Smacallan		daddr += dstpitch;
496f787bc61Smacallan		h--;
497f787bc61Smacallan	}
498f787bc61Smacallan}
499f787bc61Smacallan
50076a85281Smacallan/* up to 124 pixels so direction doesn't matter, unaligned, straight copy */
50176a85281Smacallanstatic void
50272fd264fSmacallanCG14Copy8_short_norop(Cg14Ptr p, int srcstart, int dststart, int w, int h,
50372fd264fSmacallan    int srcpitch, int dstpitch)
50476a85281Smacallan{
50576a85281Smacallan	int saddr, daddr, pre, dist, wrds, swrds, spre, sreg, restaddr, post;
50676a85281Smacallan	int ssreg;
50776a85281Smacallan#ifdef DEBUG
50876a85281Smacallan	int taddr = 4 + dstpitch * 50;
50976a85281Smacallan#endif
51076a85281Smacallan	uint32_t lmask, rmask;
51176a85281Smacallan	ENTER;
51276a85281Smacallan
51376a85281Smacallan	pre = dststart & 3;
51476a85281Smacallan	lmask = 0xffffffff >> pre;
51576a85281Smacallan	spre = srcstart & 3;
51676a85281Smacallan	/*
51776a85281Smacallan	 * make sure we count all the words needed to cover the destination
51876a85281Smacallan	 * line, covering potential partials on both ends
51976a85281Smacallan	 */
52076a85281Smacallan	wrds = (w + pre + 3) >> 2;
52176a85281Smacallan	swrds = (w + spre + 3) >> 2;
52276a85281Smacallan
52376a85281Smacallan	if (spre < pre) {
52476a85281Smacallan		dist = 32 - (pre - spre) * 8;
52576a85281Smacallan		sreg = 9;
52676a85281Smacallan	} else {
52776a85281Smacallan		dist = (spre - pre) * 8;
52876a85281Smacallan		sreg = 8;
52976a85281Smacallan	}
53076a85281Smacallan
53176a85281Smacallan	/*
53276a85281Smacallan	 * mask out trailing pixels to avoid partial writes
53376a85281Smacallan	 */
53476a85281Smacallan	post = (dststart + w) & 3;
53576a85281Smacallan	if (post != 0) {
53676a85281Smacallan		rmask = ~(0xffffffff >> (post * 8));
53776a85281Smacallan		write_sx_reg(p, SX_QUEUED(7), rmask);
53876a85281Smacallan		write_sx_reg(p, SX_QUEUED(6), ~rmask);
53976a85281Smacallan	}
54076a85281Smacallan
54176a85281Smacallan	DPRINTF(X_ERROR, "%s %d %d, %d %d %08x %d %d %d %d %08x\n", __func__,
54276a85281Smacallan	    w, h, spre, pre, lmask, dist, sreg, wrds, post, rmask);
54376a85281Smacallan
54476a85281Smacallan	/* mask out the leading pixels in dst by using a mask and ROP */
54576a85281Smacallan	if (pre != 0) {
546c1537409Smacallan		CG14Wait(p);
54776a85281Smacallan		write_sx_reg(p, SX_ROP_CONTROL, 0xca);
54876a85281Smacallan		write_sx_reg(p, SX_QUEUED(R_MASK), lmask);
54976a85281Smacallan	}
55076a85281Smacallan
55176a85281Smacallan	saddr = srcstart & ~3;
55276a85281Smacallan	daddr = dststart & ~3;
55376a85281Smacallan
55476a85281Smacallan	while (h > 0) {
55572fd264fSmacallan		sxm(SX_LD, saddr, sreg, swrds - 1);
55676a85281Smacallan		if (wrds > 15) {
55776a85281Smacallan			if (dist != 0) {
55872fd264fSmacallan				sxi(SX_FUNNEL_I(8, dist, 40, 15));
55972fd264fSmacallan				sxi(SX_FUNNEL_I(24, dist, 56, wrds - 16));
56072fd264fSmacallan				/* shifted source pixels are now at reg 40+ */
56176a85281Smacallan				ssreg = 40;
56276a85281Smacallan			} else ssreg = 8;
56376a85281Smacallan			if (pre != 0) {
56476a85281Smacallan				/* read only the first word */
56572fd264fSmacallan				sxm(SX_LD, daddr, 80, 0);
56676a85281Smacallan				/* mask out leading junk */
56772fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, ssreg, 0));
56876a85281Smacallan			}
56976a85281Smacallan		} else {
57076a85281Smacallan			if (dist != 0) {
57172fd264fSmacallan				sxi(SX_FUNNEL_I(8, dist, 40, wrds));
57276a85281Smacallan				ssreg = 40;
57376a85281Smacallan			} else ssreg = 8;
57476a85281Smacallan			if (pre != 0) {
57576a85281Smacallan				/* read only the first word */
57672fd264fSmacallan				sxm(SX_LD, daddr, 80, 0);
57776a85281Smacallan				/* mask out leading junk */
57872fd264fSmacallan				sxi(SX_ROPB(ssreg, 80, ssreg, 0));
57976a85281Smacallan			}
58076a85281Smacallan		}
58176a85281Smacallan		if (post != 0) {
58276a85281Smacallan			int laddr = daddr + ((wrds - 1) << 2);
58376a85281Smacallan			/*
58476a85281Smacallan			 * if the last word to be written out is a partial we
58576a85281Smacallan			 * mask out the leftovers and replace them with
58676a85281Smacallan			 * background pixels
58776a85281Smacallan			 * we could pull the same ROP * mask trick as we do on
58876a85281Smacallan			 * the left end but it's less annoying this way and
58976a85281Smacallan			 * the instruction count is the same
59076a85281Smacallan			 */
59172fd264fSmacallan			sxm(SX_LD, laddr, 81, 0);
59272fd264fSmacallan			sxi(SX_ANDS(ssreg + wrds - 1, 7, 5, 0));
59372fd264fSmacallan			sxi(SX_ANDS(81, 6, 4, 0));
59472fd264fSmacallan			sxi(SX_ORS(5, 4, ssreg + wrds - 1, 0));
59576a85281Smacallan		}
59676a85281Smacallan#ifdef DEBUG
59772fd264fSmacallan		sxm(SX_ST, taddr, 40, wrds - 1);
59876a85281Smacallan		taddr += dstpitch;
59976a85281Smacallan#endif
60072fd264fSmacallan		sxm(SX_ST, daddr, ssreg, wrds - 1);
60176a85281Smacallan		saddr += srcpitch;
60276a85281Smacallan		daddr += dstpitch;
60376a85281Smacallan		h--;
60476a85281Smacallan	}
60576a85281Smacallan}
60676a85281Smacallan
607f71acd79Smacallanstatic void
608f71acd79SmacallanCG14Copy8(PixmapPtr pDstPixmap,
609f71acd79Smacallan         int srcX, int srcY, int dstX, int dstY, int w, int h)
610f71acd79Smacallan{
611f71acd79Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
612f71acd79Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
613f71acd79Smacallan	int dstpitch, dstoff, srcpitch, srcoff;
614f71acd79Smacallan	int srcstart, dststart, xinc, srcinc, dstinc;
615f71acd79Smacallan	int line, count, s, d, num;
616f71acd79Smacallan
617f71acd79Smacallan	ENTER;
618f71acd79Smacallan	dstpitch = exaGetPixmapPitch(pDstPixmap);
619f71acd79Smacallan	dstoff = exaGetPixmapOffset(pDstPixmap);
620f71acd79Smacallan	srcpitch = p->srcpitch;
621f71acd79Smacallan	srcoff = p->srcoff;
622f71acd79Smacallan	/*
623f71acd79Smacallan	 * should clear the WO bit in SX_CONTROL_STATUS, then check if SX
624f71acd79Smacallan	 * actually wrote anything and only sync if it did
625f71acd79Smacallan	 */
626f71acd79Smacallan	srcstart = srcX + (srcpitch * srcY) + srcoff;
627f71acd79Smacallan	dststart = dstX + (dstpitch * dstY) + dstoff;
628f71acd79Smacallan
629f71acd79Smacallan	if (p->ydir < 0) {
630f71acd79Smacallan		srcstart += (h - 1) * srcpitch;
631f71acd79Smacallan		dststart += (h - 1) * dstpitch;
632f71acd79Smacallan		srcinc = -srcpitch;
633f71acd79Smacallan		dstinc = -dstpitch;
634f71acd79Smacallan	} else {
635f71acd79Smacallan		srcinc = srcpitch;
636f71acd79Smacallan		dstinc = dstpitch;
637f71acd79Smacallan	}
638f787bc61Smacallan
639f787bc61Smacallan	/*
640f787bc61Smacallan	 * this copies up to 124 pixels wide in one go, so horizontal
641f787bc61Smacallan	 * direction / overlap don't matter
642f787bc61Smacallan	 * uses all 32bit accesses and funnel shifter for unaligned copies
643f787bc61Smacallan	 */
644f787bc61Smacallan	if ((w < 125) && (w > 8)) {
64576a85281Smacallan		switch (p->last_rop) {
64676a85281Smacallan			case 0xcc:
64772fd264fSmacallan				CG14Copy8_short_norop(p,
64872fd264fSmacallan				    srcstart, dststart, w, h, srcinc, dstinc);
64976a85281Smacallan				break;
65076a85281Smacallan			default:
65172fd264fSmacallan				CG14Copy8_short_rop(p,
65272fd264fSmacallan				    srcstart, dststart, w, h, srcinc, dstinc);
65376a85281Smacallan		}
654f787bc61Smacallan		return;
655f787bc61Smacallan	}
656f787bc61Smacallan
657f787bc61Smacallan	/*
658f787bc61Smacallan	 * only invert x direction if absolutely necessary, it's a pain to
659f787bc61Smacallan	 * go backwards on SX so avoid as much as possible
660f787bc61Smacallan	 */
661f787bc61Smacallan	if ((p->xdir < 0) && (srcoff == dstoff) && (srcY == dstY)) {
662f787bc61Smacallan		xinc = -32;
663f787bc61Smacallan	} else
664f787bc61Smacallan		xinc = 32;
665f787bc61Smacallan
666f787bc61Smacallan	/*
667f787bc61Smacallan	 * for aligned copies we can go all 32bit and avoid VRAM reads in the
668f787bc61Smacallan	 * most common case
669f787bc61Smacallan	 */
67081c68cf8Smacallan	if (((srcstart & 3) == (dststart & 3)) && (xinc > 0)) {
67181c68cf8Smacallan		switch (p->last_rop) {
67281c68cf8Smacallan			case 0xcc:
67372fd264fSmacallan				CG14Copy8_aligned_norop(p,
67472fd264fSmacallan				    srcstart, dststart, w, h, srcinc, dstinc);
67581c68cf8Smacallan				break;
67681c68cf8Smacallan			default:
67772fd264fSmacallan				CG14Copy8_aligned_rop(p,
67872fd264fSmacallan				    srcstart, dststart, w, h, srcinc, dstinc);
67981c68cf8Smacallan		}
68081c68cf8Smacallan		return;
68181c68cf8Smacallan	}
682f787bc61Smacallan
68386527ef6Smacallan	/*
68486527ef6Smacallan	 * if we make it here we either have something large and unaligned,
68586527ef6Smacallan	 * something we need to do right to left, or something tiny.
68686527ef6Smacallan	 * we handle the non-tiny cases by breaking them down into chunks that
68786527ef6Smacallan	 * Copy8_short_*() can handle, making sure the destinations are 32bit
68886527ef6Smacallan	 * aligned whenever possible
68986527ef6Smacallan	 * since we copy by block, not by line we need to go backwards even if
69086527ef6Smacallan	 * we don't copy within the same line
69186527ef6Smacallan	 */
69286527ef6Smacallan	if (w > 8) {
69386527ef6Smacallan		int next, wi, end = dststart + w;
69472fd264fSmacallan		DPRINTF(X_ERROR, "%s %08x %08x %d\n",
69572fd264fSmacallan		    __func__, srcstart, dststart, w);
69686527ef6Smacallan		if ((p->xdir < 0) && (srcoff == dstoff)) {
69786527ef6Smacallan			srcstart += w;
69886527ef6Smacallan			next = max((end - 120) & ~3, dststart);
69986527ef6Smacallan			wi = end - next;
70086527ef6Smacallan			srcstart -= wi;
70186527ef6Smacallan			while (wi > 0) {
70272fd264fSmacallan				DPRINTF(X_ERROR, "%s RL %08x %08x %d\n",
70372fd264fSmacallan				    __func__, srcstart, next, wi);
70486527ef6Smacallan				if (p->last_rop == 0xcc) {
70572fd264fSmacallan					CG14Copy8_short_norop(p, srcstart,
70672fd264fSmacallan					    next, wi, h, srcinc, dstinc);
70786527ef6Smacallan				} else
70872fd264fSmacallan					CG14Copy8_short_rop(p, srcstart,
70972fd264fSmacallan					    next, wi, h, srcinc, dstinc);
71086527ef6Smacallan				end = next;
71186527ef6Smacallan				/*
71286527ef6Smacallan				 * avoid extremely narrow copies so I don't
71386527ef6Smacallan				 * have to deal with dangling start and end
71486527ef6Smacallan				 * pixels in the same word
71586527ef6Smacallan				 */
71686527ef6Smacallan				if ((end - dststart) < 140) {
71786527ef6Smacallan					next = max((end - 80) & ~3, dststart);
71886527ef6Smacallan				} else {
71986527ef6Smacallan					next = max((end - 120) & ~3, dststart);
72086527ef6Smacallan				}
72186527ef6Smacallan				wi = end - next;
72286527ef6Smacallan				srcstart -= wi;
72386527ef6Smacallan			}
72486527ef6Smacallan		} else {
72586527ef6Smacallan			next = min(end, (dststart + 124) & ~3);
72686527ef6Smacallan			wi = next - dststart;
72786527ef6Smacallan			while (wi > 0) {
72872fd264fSmacallan				DPRINTF(X_ERROR, "%s LR %08x %08x %d\n",
72972fd264fSmacallan				    __func__, srcstart, next, wi);
73086527ef6Smacallan				if (p->last_rop == 0xcc) {
73172fd264fSmacallan					CG14Copy8_short_norop(p,
73272fd264fSmacallan					    srcstart, dststart, wi, h,
73372fd264fSmacallan					    srcinc, dstinc);
73486527ef6Smacallan				} else
73572fd264fSmacallan					CG14Copy8_short_rop(p,
73672fd264fSmacallan					    srcstart, dststart, wi, h,
73772fd264fSmacallan					    srcinc, dstinc);
73886527ef6Smacallan				srcstart += wi;
73986527ef6Smacallan				dststart = next;
74086527ef6Smacallan				if ((end - dststart) < 140) {
74186527ef6Smacallan					next = min(end, (dststart + 84) & ~3);
74286527ef6Smacallan				} else {
74386527ef6Smacallan					next = min(end, (dststart + 124) & ~3);
74486527ef6Smacallan				}
74586527ef6Smacallan				wi = next - dststart;
74686527ef6Smacallan			}
74786527ef6Smacallan		}
74886527ef6Smacallan		return;
74986527ef6Smacallan	}
75086527ef6Smacallan	if (xinc < 0) {
75186527ef6Smacallan		srcstart += (w - 32);
75286527ef6Smacallan		dststart += (w - 32);
75386527ef6Smacallan	}
75486527ef6Smacallan
75586527ef6Smacallan	DPRINTF(X_ERROR, "%s fallback to byte-wise %d %d\n", __func__, w, h);
756f71acd79Smacallan	if (p->last_rop == 0xcc) {
757f71acd79Smacallan		/* plain old copy */
758f71acd79Smacallan		if ( xinc > 0) {
759f71acd79Smacallan			/* going left to right */
760f71acd79Smacallan			for (line = 0; line < h; line++) {
761f71acd79Smacallan				count = 0;
762f71acd79Smacallan				s = srcstart;
763f71acd79Smacallan				d = dststart;
764f71acd79Smacallan				while ( count < w) {
765f71acd79Smacallan					num = min(32, w - count);
76672fd264fSmacallan					sxm(SX_LDB, s, 10, num - 1);
76772fd264fSmacallan					sxm(SX_STBM, d, 10, num - 1);
768f71acd79Smacallan					s += xinc;
769f71acd79Smacallan					d += xinc;
770f71acd79Smacallan					count += 32;
771f71acd79Smacallan				}
772f71acd79Smacallan				srcstart += srcinc;
773f71acd79Smacallan				dststart += dstinc;
774f71acd79Smacallan			}
775f71acd79Smacallan		} else {
776f71acd79Smacallan			/* going right to left */
777f71acd79Smacallan			int i, chunks = (w >> 5);
778f71acd79Smacallan			for (line = 0; line < h; line++) {
779f71acd79Smacallan				s = srcstart;
780f71acd79Smacallan				d = dststart;
781f71acd79Smacallan				count = w;
782f71acd79Smacallan				for (i = 0; i < chunks; i++) {
78372fd264fSmacallan					sxm(SX_LDB, s, 10, 31);
78472fd264fSmacallan					sxm(SX_STBM, d, 10, 31);
785f71acd79Smacallan					s -= 32;
786f71acd79Smacallan					d -= 32;
787f71acd79Smacallan					count -= 32;
788f71acd79Smacallan				}
789f71acd79Smacallan				/* leftovers, if any */
790f71acd79Smacallan				if (count > 0) {
791f71acd79Smacallan					s += (32 - count);
792f71acd79Smacallan					d += (32 - count);
79372fd264fSmacallan					sxm(SX_LDB, s, 10, count - 1);
79472fd264fSmacallan					sxm(SX_STBM, d, 10, count - 1);
795f71acd79Smacallan				}
796f71acd79Smacallan				srcstart += srcinc;
797f71acd79Smacallan				dststart += dstinc;
798f71acd79Smacallan			}
799f71acd79Smacallan		}
800f71acd79Smacallan	} else {
801f71acd79Smacallan		/* ROPs needed */
802f71acd79Smacallan		if ( xinc > 0) {
803f71acd79Smacallan			/* going left to right */
804f71acd79Smacallan			for (line = 0; line < h; line++) {
805f71acd79Smacallan				count = 0;
806f71acd79Smacallan				s = srcstart;
807f71acd79Smacallan				d = dststart;
808f71acd79Smacallan				while ( count < w) {
809f71acd79Smacallan					num = min(32, w - count);
81072fd264fSmacallan					sxm(SX_LDB, s, 10, num - 1);
81172fd264fSmacallan					sxm(SX_LDB, d, 42, num - 1);
812f71acd79Smacallan					if (num > 16) {
81372fd264fSmacallan						sxi(SX_ROP(10, 42, 74, 15));
81472fd264fSmacallan						sxi(SX_ROP(26, 58, 90, num - 17));
815f71acd79Smacallan					} else {
81672fd264fSmacallan						sxi(SX_ROP(10, 42, 74, num - 1));
817f71acd79Smacallan					}
81872fd264fSmacallan					sxm(SX_STBM, d, 74, num - 1);
819f71acd79Smacallan					s += xinc;
820f71acd79Smacallan					d += xinc;
821f71acd79Smacallan					count += 32;
822f71acd79Smacallan				}
823f71acd79Smacallan				srcstart += srcinc;
824f71acd79Smacallan				dststart += dstinc;
825f71acd79Smacallan			}
826f71acd79Smacallan		} else {
827f71acd79Smacallan			/* going right to left */
828f71acd79Smacallan			int i, chunks = (w >> 5);
829f71acd79Smacallan			for (line = 0; line < h; line++) {
830f71acd79Smacallan				s = srcstart;
831f71acd79Smacallan				d = dststart;
832f71acd79Smacallan				count = w;
833f71acd79Smacallan				for (i = 0; i < chunks; i++) {
83472fd264fSmacallan					sxm(SX_LDB, s, 10, 31);
83572fd264fSmacallan					sxm(SX_LDB, d, 42, 31);
83672fd264fSmacallan					sxi(SX_ROP(10, 42, 74, 15));
83772fd264fSmacallan					sxi(SX_ROP(26, 58, 90, 15));
83872fd264fSmacallan					sxm(SX_STBM, d, 74, 31);
839f71acd79Smacallan					s -= 128;
840f71acd79Smacallan					d -= 128;
841f71acd79Smacallan					count -= 32;
842f71acd79Smacallan				}
843f71acd79Smacallan				/* leftovers, if any */
844f71acd79Smacallan				if (count > 0) {
845f71acd79Smacallan					s += (32 - count);
846f71acd79Smacallan					d += (32 - count);
84772fd264fSmacallan					sxm(SX_LDB, s, 10, count - 1);
84872fd264fSmacallan					sxm(SX_LDB, d, 42, count - 1);
849f71acd79Smacallan					if (count > 16) {
85072fd264fSmacallan						sxi(SX_ROP(10, 42, 74, 15));
85172fd264fSmacallan						sxi(SX_ROP(26, 58, 90, count - 17));
852f71acd79Smacallan					} else {
85372fd264fSmacallan						sxi(SX_ROP(10, 42, 74, count - 1));
854f71acd79Smacallan					}
85572fd264fSmacallan					sxm(SX_STBM, d, 74, count - 1);
856f71acd79Smacallan				}
857f71acd79Smacallan				srcstart += srcinc;
858f71acd79Smacallan				dststart += dstinc;
859f71acd79Smacallan			}
860f71acd79Smacallan		}
861f71acd79Smacallan	}
862f71acd79Smacallan	exaMarkSync(pDstPixmap->drawable.pScreen);
863f71acd79Smacallan}
864f71acd79Smacallan
8654261fa58Smacallanstatic void
8664261fa58SmacallanCG14DoneCopy(PixmapPtr pDstPixmap)
8674261fa58Smacallan{
8684261fa58Smacallan}
8694261fa58Smacallan
8704261fa58Smacallanstatic Bool
8714261fa58SmacallanCG14PrepareSolid(PixmapPtr pPixmap, int alu, Pixel planemask, Pixel fg)
8724261fa58Smacallan{
8734261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
8744261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
8754261fa58Smacallan
8764261fa58Smacallan	ENTER;
877faf11d72Schristos	DPRINTF(X_ERROR, "bits per pixel: %d %08lx\n",
878b8ad197aSmacallan	    pPixmap->drawable.bitsPerPixel, fg);
879b8ad197aSmacallan
880dbf8597cSmacallan	/*
881dbf8597cSmacallan	 * GXset and GXclear are really just specual cases of GXcopy with
882dbf8597cSmacallan	 * fixed fill colour
883dbf8597cSmacallan	 */
884dbf8597cSmacallan	switch (alu) {
885dbf8597cSmacallan		case GXclear:
886dbf8597cSmacallan			alu = GXcopy;
887dbf8597cSmacallan			fg = 0;
888dbf8597cSmacallan			break;
889dbf8597cSmacallan		case GXset:
890dbf8597cSmacallan			alu = GXcopy;
891dbf8597cSmacallan			fg = 0xffffffff;
892dbf8597cSmacallan			break;
893dbf8597cSmacallan	}
894b8ad197aSmacallan	/* repeat the colour in every sub byte if we're in 8 bit */
895b8ad197aSmacallan	if (pPixmap->drawable.bitsPerPixel == 8) {
896b8ad197aSmacallan		fg |= fg << 8;
897b8ad197aSmacallan		fg |= fg << 16;
898b8ad197aSmacallan	}
8994261fa58Smacallan	write_sx_reg(p, SX_QUEUED(8), fg);
9004261fa58Smacallan	write_sx_reg(p, SX_QUEUED(9), fg);
9014261fa58Smacallan	if (planemask != p->last_mask) {
9024261fa58Smacallan		CG14Wait(p);
9034261fa58Smacallan		write_sx_reg(p, SX_PLANEMASK, planemask);
9044261fa58Smacallan		p->last_mask = planemask;
9054261fa58Smacallan	}
9064261fa58Smacallan	alu = sx_rop[alu];
9074261fa58Smacallan	if (alu != p->last_rop) {
9084261fa58Smacallan		CG14Wait(p);
9094261fa58Smacallan		write_sx_reg(p, SX_ROP_CONTROL, alu);
9104261fa58Smacallan		p->last_rop = alu;
9114261fa58Smacallan	}
912dbf8597cSmacallan
9134261fa58Smacallan	DPRINTF(X_ERROR, "%s: %x\n", __func__, alu);
9144261fa58Smacallan	return TRUE;
9154261fa58Smacallan}
9164261fa58Smacallan
9174261fa58Smacallanstatic void
9184261fa58SmacallanCG14Solid32(Cg14Ptr p, uint32_t start, uint32_t pitch, int w, int h)
9194261fa58Smacallan{
9204261fa58Smacallan	int line, x, num;
9214261fa58Smacallan	uint32_t ptr;
9224261fa58Smacallan
9234261fa58Smacallan	ENTER;
9244261fa58Smacallan	if (p->last_rop == 0xcc) {
9254261fa58Smacallan		/* simple fill */
9264261fa58Smacallan		for (line = 0; line < h; line++) {
9274261fa58Smacallan			x = 0;
9284261fa58Smacallan			while (x < w) {
9294261fa58Smacallan				ptr = start + (x << 2);
9304261fa58Smacallan				num = min(32, w - x);
93172fd264fSmacallan				sxm(SX_STS, ptr, 8, num - 1);
9324261fa58Smacallan				x += 32;
9334261fa58Smacallan			}
9344261fa58Smacallan			start += pitch;
9354261fa58Smacallan		}
9364261fa58Smacallan	} else if (p->last_rop == 0xaa) {
9374261fa58Smacallan		/* nothing to do here */
9384261fa58Smacallan		return;
9394261fa58Smacallan	} else {
9404261fa58Smacallan		/* alright, let's do actual ROP stuff */
9414261fa58Smacallan
9424261fa58Smacallan		/* first repeat the fill colour into 16 registers */
94372fd264fSmacallan		sxi(SX_SELECT_S(8, 8, 10, 15));
9444261fa58Smacallan
9454261fa58Smacallan		for (line = 0; line < h; line++) {
9464261fa58Smacallan			x = 0;
9474261fa58Smacallan			while (x < w) {
9484261fa58Smacallan				ptr = start + (x << 2);
9494261fa58Smacallan				num = min(32, w - x);
9504261fa58Smacallan				/* now suck fb data into registers */
95172fd264fSmacallan				sxm(SX_LD, ptr, 42, num - 1);
9524261fa58Smacallan				/*
9534261fa58Smacallan				 * ROP them with the fill data we left in 10
9544261fa58Smacallan				 * non-memory ops can only have counts up to 16
9554261fa58Smacallan				 */
9564261fa58Smacallan				if (num <= 16) {
95772fd264fSmacallan					sxi(SX_ROP(10, 42, 74, num - 1));
9584261fa58Smacallan				} else {
95972fd264fSmacallan					sxi(SX_ROP(10, 42, 74, 15));
96072fd264fSmacallan					sxi(SX_ROP(10, 58, 90, num - 17));
9614261fa58Smacallan				}
9624261fa58Smacallan				/* and write the result back into memory */
96372fd264fSmacallan				sxm(SX_ST, ptr, 74, num - 1);
9644261fa58Smacallan				x += 32;
9654261fa58Smacallan			}
9664261fa58Smacallan			start += pitch;
9674261fa58Smacallan		}
9684261fa58Smacallan	}
9694261fa58Smacallan}
9704261fa58Smacallan
9714261fa58Smacallanstatic void
9724261fa58SmacallanCG14Solid8(Cg14Ptr p, uint32_t start, uint32_t pitch, int w, int h)
9734261fa58Smacallan{
974dbf8597cSmacallan	int line, num, pre, cnt;
9754261fa58Smacallan	uint32_t ptr;
9764261fa58Smacallan
9774261fa58Smacallan	ENTER;
978b8ad197aSmacallan	pre = start & 3;
979b8ad197aSmacallan	if (pre != 0) pre = 4 - pre;
9804261fa58Smacallan
9814261fa58Smacallan	if (p->last_rop == 0xcc) {
9824261fa58Smacallan		/* simple fill */
9834261fa58Smacallan		for (line = 0; line < h; line++) {
984b8ad197aSmacallan			ptr = start;
985b8ad197aSmacallan			cnt = w;
986b46cab2aSmacallan			pre = min(pre, cnt);
987b8ad197aSmacallan			if (pre) {
98872fd264fSmacallan				sxm(SX_STBS, ptr, 8, pre - 1);
989b8ad197aSmacallan				ptr += pre;
990b8ad197aSmacallan				cnt -= pre;
991b46cab2aSmacallan				if (cnt == 0) goto next;
992b8ad197aSmacallan			}
993b8ad197aSmacallan			/* now do the aligned pixels in 32bit chunks */
994b8ad197aSmacallan			if (ptr & 3) xf86Msg(X_ERROR, "%s %x\n", __func__, ptr);
995b8ad197aSmacallan			while(cnt > 3) {
996b8ad197aSmacallan				num = min(32, cnt >> 2);
99772fd264fSmacallan				sxm(SX_STS, ptr, 8, num - 1);
998b8ad197aSmacallan				ptr += num << 2;
999b8ad197aSmacallan				cnt -= num << 2;
1000b8ad197aSmacallan			}
1001b8ad197aSmacallan			if (cnt > 3) xf86Msg(X_ERROR, "%s cnt %d\n", __func__, cnt);
1002b8ad197aSmacallan			if (cnt > 0) {
100372fd264fSmacallan				sxm(SX_STBS, ptr, 8, cnt - 1);
10044261fa58Smacallan			}
1005b8ad197aSmacallan			if ((ptr + cnt) != (start + w)) xf86Msg(X_ERROR, "%s %x vs %x\n", __func__, ptr + cnt, start + w);
1006b46cab2aSmacallannext:
10074261fa58Smacallan			start += pitch;
10084261fa58Smacallan		}
10094261fa58Smacallan	} else if (p->last_rop == 0xaa) {
10104261fa58Smacallan		/* nothing to do here */
10114261fa58Smacallan		return;
10124261fa58Smacallan	} else {
10134261fa58Smacallan		/* alright, let's do actual ROP stuff */
10144261fa58Smacallan
10154261fa58Smacallan		/* first repeat the fill colour into 16 registers */
101672fd264fSmacallan		sxi(SX_SELECT_S(8, 8, 10, 15));
10174261fa58Smacallan
10184261fa58Smacallan		for (line = 0; line < h; line++) {
1019dbf8597cSmacallan			ptr = start;
1020dbf8597cSmacallan			cnt = w;
1021dbf8597cSmacallan			pre = min(pre, cnt);
1022dbf8597cSmacallan			if (pre) {
102372fd264fSmacallan				sxm(SX_LDB, ptr, 26, pre - 1);
102472fd264fSmacallan				sxi(SX_ROP(10, 26, 42, pre - 1));
102572fd264fSmacallan				sxm(SX_STB, ptr, 42, pre - 1);
1026dbf8597cSmacallan				ptr += pre;
1027dbf8597cSmacallan				cnt -= pre;
1028dbf8597cSmacallan				if (cnt == 0) goto next2;
1029dbf8597cSmacallan			}
1030dbf8597cSmacallan			/* now do the aligned pixels in 32bit chunks */
1031dbf8597cSmacallan			if (ptr & 3) xf86Msg(X_ERROR, "%s %x\n", __func__, ptr);
1032dbf8597cSmacallan			while(cnt > 3) {
1033dbf8597cSmacallan				num = min(32, cnt >> 2);
103472fd264fSmacallan				sxm(SX_LD, ptr, 26, num - 1);
10354261fa58Smacallan				if (num <= 16) {
103672fd264fSmacallan					sxi(SX_ROP(10, 26, 58, num - 1));
10374261fa58Smacallan				} else {
103872fd264fSmacallan					sxi(SX_ROP(10, 26, 58, 15));
103972fd264fSmacallan					sxi(SX_ROP(10, 42, 74, num - 17));
10404261fa58Smacallan				}
104172fd264fSmacallan				sxm(SX_ST, ptr, 58, num - 1);
1042dbf8597cSmacallan				ptr += num << 2;
1043dbf8597cSmacallan				cnt -= num << 2;
10444261fa58Smacallan			}
1045dbf8597cSmacallan			if (cnt > 3) xf86Msg(X_ERROR, "%s cnt %d\n", __func__, cnt);
1046dbf8597cSmacallan			if (cnt > 0) {
104772fd264fSmacallan				sxm(SX_LDB, ptr, 26, cnt - 1);
104872fd264fSmacallan				sxi(SX_ROP(10, 26, 42, cnt - 1));
104972fd264fSmacallan				sxm(SX_STB, ptr, 42, cnt - 1);
1050dbf8597cSmacallan			}
1051dbf8597cSmacallan			if ((ptr + cnt) != (start + w)) xf86Msg(X_ERROR, "%s %x vs %x\n", __func__, ptr + cnt, start + w);
1052dbf8597cSmacallannext2:
10534261fa58Smacallan			start += pitch;
10544261fa58Smacallan		}
10554261fa58Smacallan	}
10564261fa58Smacallan}
10574261fa58Smacallan
10584261fa58Smacallanstatic void
10594261fa58SmacallanCG14Solid(PixmapPtr pPixmap, int x1, int y1, int x2, int y2)
10604261fa58Smacallan{
10614261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
10624261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
10634261fa58Smacallan	int w = x2 - x1, h = y2 - y1, dstoff, dstpitch;
10644261fa58Smacallan	int start, depth;
10654261fa58Smacallan
10664261fa58Smacallan	ENTER;
10674261fa58Smacallan	dstpitch = exaGetPixmapPitch(pPixmap);
10684261fa58Smacallan	dstoff = exaGetPixmapOffset(pPixmap);
10694261fa58Smacallan
10704261fa58Smacallan	depth = pPixmap->drawable.bitsPerPixel;
10714261fa58Smacallan	switch (depth) {
10724261fa58Smacallan		case 32:
10734261fa58Smacallan			start = dstoff + (y1 * dstpitch) + (x1 << 2);
10744261fa58Smacallan			CG14Solid32(p, start, dstpitch, w, h);
10754261fa58Smacallan			break;
10764261fa58Smacallan		case 8:
10774261fa58Smacallan			start = dstoff + (y1 * dstpitch) + x1;
10784261fa58Smacallan			CG14Solid8(p, start, dstpitch, w, h);
10794261fa58Smacallan			break;
10804261fa58Smacallan	}
10814261fa58Smacallan
10824261fa58Smacallan	DPRINTF(X_ERROR, "Solid %d %d %d %d, %d %d -> %d\n", x1, y1, x2, y2,
10834261fa58Smacallan	    dstpitch, dstoff, start);
10844261fa58Smacallan	DPRINTF(X_ERROR, "%x %x %x\n", p->last_rop,
10854261fa58Smacallan	    read_sx_reg(p, SX_QUEUED(8)), read_sx_reg(p, SX_QUEUED(9)));
10864261fa58Smacallan	exaMarkSync(pPixmap->drawable.pScreen);
10874261fa58Smacallan}
10884261fa58Smacallan
10894261fa58Smacallan/*
10904261fa58Smacallan * Memcpy-based UTS.
10914261fa58Smacallan */
10924261fa58Smacallanstatic Bool
10934261fa58SmacallanCG14UploadToScreen(PixmapPtr pDst, int x, int y, int w, int h,
10944261fa58Smacallan    char *src, int src_pitch)
10954261fa58Smacallan{
10964261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
10974261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
10984261fa58Smacallan	char  *dst        = p->fb + exaGetPixmapOffset(pDst);
10994261fa58Smacallan	int    dst_pitch  = exaGetPixmapPitch(pDst);
11004261fa58Smacallan
11014261fa58Smacallan	int bpp    = pDst->drawable.bitsPerPixel;
11024261fa58Smacallan	int cpp    = (bpp + 7) >> 3;
11034261fa58Smacallan	int wBytes = w * cpp;
11044261fa58Smacallan
11054261fa58Smacallan	ENTER;
1106f71acd79Smacallan	DPRINTF(X_ERROR, "%s depth %d\n", __func__, bpp);
11074261fa58Smacallan	dst += (x * cpp) + (y * dst_pitch);
11084261fa58Smacallan
11094261fa58Smacallan	CG14Wait(p);
11104261fa58Smacallan
11114261fa58Smacallan	while (h--) {
11124261fa58Smacallan		memcpy(dst, src, wBytes);
11134261fa58Smacallan		src += src_pitch;
11144261fa58Smacallan		dst += dst_pitch;
11154261fa58Smacallan	}
11164261fa58Smacallan	__asm("stbar;");
11174261fa58Smacallan	return TRUE;
11184261fa58Smacallan}
11194261fa58Smacallan
11204261fa58Smacallan/*
11214261fa58Smacallan * Memcpy-based DFS.
11224261fa58Smacallan */
11234261fa58Smacallanstatic Bool
11244261fa58SmacallanCG14DownloadFromScreen(PixmapPtr pSrc, int x, int y, int w, int h,
11254261fa58Smacallan    char *dst, int dst_pitch)
11264261fa58Smacallan{
11274261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pSrc->drawable.pScreen->myNum];
11284261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
11294261fa58Smacallan	char  *src        = p->fb + exaGetPixmapOffset(pSrc);
11304261fa58Smacallan	int    src_pitch  = exaGetPixmapPitch(pSrc);
11314261fa58Smacallan
11324261fa58Smacallan	ENTER;
11334261fa58Smacallan	int bpp    = pSrc->drawable.bitsPerPixel;
11344261fa58Smacallan	int cpp    = (bpp + 7) >> 3;
11354261fa58Smacallan	int wBytes = w * cpp;
11364261fa58Smacallan
11374261fa58Smacallan	src += (x * cpp) + (y * src_pitch);
11384261fa58Smacallan
11394261fa58Smacallan	CG14Wait(p);
11404261fa58Smacallan
11414261fa58Smacallan	while (h--) {
11424261fa58Smacallan		memcpy(dst, src, wBytes);
11434261fa58Smacallan		src += src_pitch;
11444261fa58Smacallan		dst += dst_pitch;
11454261fa58Smacallan	}
11464261fa58Smacallan
11474261fa58Smacallan	return TRUE;
11484261fa58Smacallan}
11494261fa58Smacallan
11504261fa58SmacallanBool
11514261fa58SmacallanCG14CheckComposite(int op, PicturePtr pSrcPicture,
11524261fa58Smacallan                           PicturePtr pMaskPicture,
11534261fa58Smacallan                           PicturePtr pDstPicture)
11544261fa58Smacallan{
11554261fa58Smacallan	int i, ok = FALSE;
11564261fa58Smacallan
11574261fa58Smacallan	ENTER;
11584261fa58Smacallan
11594261fa58Smacallan	/*
11604261fa58Smacallan	 * SX is in theory capable of accelerating pretty much all Xrender ops,
11614261fa58Smacallan	 * even coordinate transformation and gradients. Support will be added
11624261fa58Smacallan	 * over time and likely have to spill over into its own source file.
11634261fa58Smacallan	 */
11644261fa58Smacallan
1165a3a2ba44Smacallan	if ((op != PictOpOver) && (op != PictOpAdd) && (op != PictOpSrc)) {
1166fe97f391Smacallan		DPRINTF(X_ERROR, "%s: rejecting %d\n", __func__, op);
11674261fa58Smacallan		return FALSE;
11684261fa58Smacallan	}
11694261fa58Smacallan
11704bd47ccfSmacallan	if (pSrcPicture != NULL) {
11714bd47ccfSmacallan		i = 0;
11724bd47ccfSmacallan		while ((i < arraysize(src_formats)) && (!ok)) {
11734bd47ccfSmacallan			ok =  (pSrcPicture->format == src_formats[i]);
11744bd47ccfSmacallan			i++;
11754bd47ccfSmacallan		}
11764bd47ccfSmacallan
11774bd47ccfSmacallan		if (!ok) {
11784bd47ccfSmacallan			DPRINTF(X_ERROR, "%s: unsupported src format %x\n",
11794bd47ccfSmacallan			    __func__, pSrcPicture->format);
11804bd47ccfSmacallan			return FALSE;
11814bd47ccfSmacallan		}
11824bd47ccfSmacallan		DPRINTF(X_ERROR, "src is %x, %d\n", pSrcPicture->format, op);
11834261fa58Smacallan	}
11844261fa58Smacallan
11854bd47ccfSmacallan	if (pDstPicture != NULL) {
11864bd47ccfSmacallan		i = 0;
11874bd47ccfSmacallan		ok = FALSE;
11884bd47ccfSmacallan		while ((i < arraysize(src_formats)) && (!ok)) {
11894bd47ccfSmacallan			ok =  (pDstPicture->format == src_formats[i]);
11904bd47ccfSmacallan			i++;
11914bd47ccfSmacallan		}
11924bd47ccfSmacallan
11934bd47ccfSmacallan		if (!ok) {
11944bd47ccfSmacallan			DPRINTF(X_ERROR, "%s: unsupported dst format %x\n",
11954bd47ccfSmacallan			    __func__, pDstPicture->format);
11964bd47ccfSmacallan			return FALSE;
11974bd47ccfSmacallan		}
11984bd47ccfSmacallan		DPRINTF(X_ERROR, "dst is %x, %d\n", pDstPicture->format, op);
11994bd47ccfSmacallan	}
12004261fa58Smacallan
12014261fa58Smacallan	if (pMaskPicture != NULL) {
12024261fa58Smacallan		DPRINTF(X_ERROR, "mask is %x %d %d\n", pMaskPicture->format,
12034261fa58Smacallan		    pMaskPicture->pDrawable->width,
12044261fa58Smacallan		    pMaskPicture->pDrawable->height);
12054261fa58Smacallan	}
12064261fa58Smacallan	return TRUE;
12074261fa58Smacallan}
12084261fa58Smacallan
12094261fa58SmacallanBool
12104261fa58SmacallanCG14PrepareComposite(int op, PicturePtr pSrcPicture,
12114261fa58Smacallan                             PicturePtr pMaskPicture,
12124261fa58Smacallan                             PicturePtr pDstPicture,
12134261fa58Smacallan                             PixmapPtr  pSrc,
12144261fa58Smacallan                             PixmapPtr  pMask,
12154261fa58Smacallan                             PixmapPtr  pDst)
12164261fa58Smacallan{
12174261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
12184261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
12194261fa58Smacallan
12204261fa58Smacallan	ENTER;
12214261fa58Smacallan
1222f7cb851fSmacallan	p->no_source_pixmap = FALSE;
1223f7cb851fSmacallan	p->source_is_solid = FALSE;
1224f7cb851fSmacallan
1225a3a2ba44Smacallan	if (pSrcPicture->format == PICT_a1) {
12266bdc2ffdSmacallan		xf86Msg(X_ERROR, "src mono, dst %x, op %d\n",
12276bdc2ffdSmacallan		    pDstPicture->format, op);
1228a3a2ba44Smacallan		if (pMaskPicture != NULL) {
1229a3a2ba44Smacallan			xf86Msg(X_ERROR, "msk %x\n", pMaskPicture->format);
1230a3a2ba44Smacallan		}
1231f7cb851fSmacallan	}
12324261fa58Smacallan	if (pSrcPicture->pSourcePict != NULL) {
12334261fa58Smacallan		if (pSrcPicture->pSourcePict->type == SourcePictTypeSolidFill) {
12344261fa58Smacallan			p->fillcolour =
12354261fa58Smacallan			    pSrcPicture->pSourcePict->solidFill.color;
1236f7cb851fSmacallan			DPRINTF(X_ERROR, "%s: solid src %08x\n",
12374261fa58Smacallan			    __func__, p->fillcolour);
1238f7cb851fSmacallan			p->no_source_pixmap = TRUE;
1239f7cb851fSmacallan			p->source_is_solid = TRUE;
12404261fa58Smacallan		}
12414261fa58Smacallan	}
12424261fa58Smacallan	if ((pMaskPicture != NULL) && (pMaskPicture->pSourcePict != NULL)) {
12434261fa58Smacallan		if (pMaskPicture->pSourcePict->type ==
12444261fa58Smacallan		    SourcePictTypeSolidFill) {
12454261fa58Smacallan			p->fillcolour =
12464261fa58Smacallan			   pMaskPicture->pSourcePict->solidFill.color;
1247a3a2ba44Smacallan			xf86Msg(X_ERROR, "%s: solid mask %08x\n",
12484261fa58Smacallan			    __func__, p->fillcolour);
12494261fa58Smacallan		}
12504261fa58Smacallan	}
12514261fa58Smacallan	if (pMaskPicture != NULL) {
1252239808baSmacallan		p->mskoff = exaGetPixmapOffset(pMask);
12534261fa58Smacallan		p->mskpitch = exaGetPixmapPitch(pMask);
12544261fa58Smacallan		p->mskformat = pMaskPicture->format;
1255a3a2ba44Smacallan	} else {
1256239808baSmacallan		p->mskoff = 0;
1257a3a2ba44Smacallan		p->mskpitch = 0;
1258a3a2ba44Smacallan		p->mskformat = 0;
12594261fa58Smacallan	}
1260f7cb851fSmacallan	if (pSrc != NULL) {
1261f7cb851fSmacallan		p->source_is_solid =
1262f7cb851fSmacallan		   ((pSrc->drawable.width == 1) && (pSrc->drawable.height == 1));
1263f7cb851fSmacallan		p->srcoff = exaGetPixmapOffset(pSrc);
1264f7cb851fSmacallan		p->srcpitch = exaGetPixmapPitch(pSrc);
1265f7cb851fSmacallan		if (p->source_is_solid) {
1266f7cb851fSmacallan			p->fillcolour = *(uint32_t *)(p->fb + p->srcoff);
1267f7cb851fSmacallan		}
1268f7cb851fSmacallan	}
12694261fa58Smacallan	p->srcformat = pSrcPicture->format;
12704261fa58Smacallan	p->dstformat = pDstPicture->format;
1271f7cb851fSmacallan
1272f7cb851fSmacallan	if (p->source_is_solid) {
1273f7cb851fSmacallan		uint32_t temp;
1274f7cb851fSmacallan
1275f7cb851fSmacallan		/* stuff source colour into SX registers, swap as needed */
1276f7cb851fSmacallan		temp = p->fillcolour;
1277f7cb851fSmacallan		switch (p->srcformat) {
1278f7cb851fSmacallan			case PICT_a8r8g8b8:
1279f7cb851fSmacallan			case PICT_x8r8g8b8:
1280f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(9), temp & 0xff);
1281f7cb851fSmacallan				temp = temp >> 8;
1282f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(10), temp & 0xff);
1283f7cb851fSmacallan				temp = temp >> 8;
1284f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(11), temp & 0xff);
1285f7cb851fSmacallan				break;
1286f7cb851fSmacallan			case PICT_a8b8g8r8:
1287f7cb851fSmacallan			case PICT_x8b8g8r8:
1288f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(11), temp & 0xff);
1289f7cb851fSmacallan				temp = temp >> 8;
1290f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(10), temp & 0xff);
1291f7cb851fSmacallan				temp = temp >> 8;
1292f7cb851fSmacallan				write_sx_reg(p, SX_QUEUED(9), temp & 0xff);
1293f7cb851fSmacallan				break;
1294f7cb851fSmacallan		}
1295f7cb851fSmacallan		write_sx_reg(p, SX_QUEUED(8), 0xff);
1296f7cb851fSmacallan	}
12974261fa58Smacallan	p->op = op;
1298a3a2ba44Smacallan	if (op == PictOpSrc) {
1299a3a2ba44Smacallan		CG14PrepareCopy(pSrc, pDst, 1, 1, GXcopy, 0xffffffff);
1300a3a2ba44Smacallan	}
13014261fa58Smacallan#ifdef SX_DEBUG
13024261fa58Smacallan	DPRINTF(X_ERROR, "%x %x -> %x\n", p->srcoff, p->mskoff,
13034261fa58Smacallan	    *(uint32_t *)(p->fb + p->srcoff));
13044261fa58Smacallan#endif
13054261fa58Smacallan	return TRUE;
13064261fa58Smacallan}
13074261fa58Smacallan
13084261fa58Smacallanvoid
13094261fa58SmacallanCG14Composite(PixmapPtr pDst, int srcX, int srcY,
13104261fa58Smacallan                              int maskX, int maskY,
13114261fa58Smacallan                              int dstX, int dstY,
13124261fa58Smacallan                              int width, int height)
13134261fa58Smacallan{
13144261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDst->drawable.pScreen->myNum];
13154261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
13164261fa58Smacallan	uint32_t dstoff, dstpitch;
13174261fa58Smacallan	uint32_t dst, msk, src;
1318e311bbeeSmacallan	int flip = 0;
13194261fa58Smacallan
13204261fa58Smacallan	ENTER;
13214261fa58Smacallan	dstoff = exaGetPixmapOffset(pDst);
13224261fa58Smacallan	dstpitch = exaGetPixmapPitch(pDst);
13234261fa58Smacallan
1324e311bbeeSmacallan	flip = (PICT_FORMAT_TYPE(p->srcformat) !=
1325e311bbeeSmacallan		PICT_FORMAT_TYPE(p->dstformat));
1326e311bbeeSmacallan
13274261fa58Smacallan	switch (p->op) {
13284261fa58Smacallan		case PictOpOver:
13294261fa58Smacallan			dst = dstoff + (dstY * dstpitch) + (dstX << 2);
13304261fa58Smacallan			DPRINTF(X_ERROR, "Over %08x %08x, %d %d\n",
13314261fa58Smacallan			    p->mskformat, p->dstformat, srcX, srcY);
1332a3a2ba44Smacallan			if (p->source_is_solid) {
1333a3a2ba44Smacallan				switch (p->mskformat) {
1334a3a2ba44Smacallan					case PICT_a8:
1335a3a2ba44Smacallan						msk = p->mskoff +
1336a3a2ba44Smacallan						    (maskY * p->mskpitch) +
1337a3a2ba44Smacallan						    maskX;
1338a3a2ba44Smacallan						CG14Comp_Over8Solid(p,
1339a3a2ba44Smacallan						    msk, p->mskpitch,
1340a3a2ba44Smacallan						    dst, dstpitch,
1341a3a2ba44Smacallan						    width, height);
1342a3a2ba44Smacallan						break;
1343a3a2ba44Smacallan					case PICT_a8r8g8b8:
1344a3a2ba44Smacallan					case PICT_a8b8g8r8:
1345a3a2ba44Smacallan						msk = p->mskoff +
1346a3a2ba44Smacallan						    (maskY * p->mskpitch) +
1347a3a2ba44Smacallan						    (maskX << 2);
1348a3a2ba44Smacallan						CG14Comp_Over32Solid(p,
1349a3a2ba44Smacallan						    msk, p->mskpitch,
1350a3a2ba44Smacallan						    dst, dstpitch,
1351a3a2ba44Smacallan						    width, height);
1352a3a2ba44Smacallan						break;
1353a3a2ba44Smacallan					default:
1354a3a2ba44Smacallan						xf86Msg(X_ERROR,
1355f71acd79Smacallan						  "unsupported mask format %08x\n", p->mskformat);
1356a3a2ba44Smacallan				}
1357a3a2ba44Smacallan			} else {
13586bdc2ffdSmacallan				DPRINTF(X_ERROR, "non-solid over with msk %x\n",
13596bdc2ffdSmacallan				    p->mskformat);
1360a3a2ba44Smacallan				switch (p->srcformat) {
1361a3a2ba44Smacallan					case PICT_a8r8g8b8:
1362a3a2ba44Smacallan					case PICT_a8b8g8r8:
1363a3a2ba44Smacallan						src = p->srcoff +
1364a3a2ba44Smacallan						    (srcY * p->srcpitch) +
1365a3a2ba44Smacallan						    (srcX << 2);
1366a3a2ba44Smacallan						dst = dstoff +
1367a3a2ba44Smacallan						    (dstY * dstpitch) +
1368a3a2ba44Smacallan						    (dstX << 2);
1369a3a2ba44Smacallan						if (p->mskformat == PICT_a8) {
1370a3a2ba44Smacallan							msk = p->mskoff +
1371a3a2ba44Smacallan							    (maskY * p->mskpitch) +
1372a3a2ba44Smacallan							    maskX;
1373a3a2ba44Smacallan							CG14Comp_Over32Mask(p,
1374a3a2ba44Smacallan							    src, p->srcpitch,
1375a3a2ba44Smacallan							    msk, p->mskpitch,
1376a3a2ba44Smacallan							    dst, dstpitch,
1377e311bbeeSmacallan							    width, height, flip);
1378a3a2ba44Smacallan						} else {
1379a3a2ba44Smacallan							CG14Comp_Over32(p,
1380a3a2ba44Smacallan							    src, p->srcpitch,
1381a3a2ba44Smacallan							    dst, dstpitch,
1382e311bbeeSmacallan							    width, height, flip);
1383a3a2ba44Smacallan						}
1384a3a2ba44Smacallan						break;
1385a3a2ba44Smacallan					case PICT_x8r8g8b8:
1386a3a2ba44Smacallan					case PICT_x8b8g8r8:
13876bdc2ffdSmacallan						src = p->srcoff +
13886bdc2ffdSmacallan						    (srcY * p->srcpitch) +
13896bdc2ffdSmacallan						    (srcX << 2);
13906bdc2ffdSmacallan						dst = dstoff +
13916bdc2ffdSmacallan						    (dstY * dstpitch) +
13926bdc2ffdSmacallan						    (dstX << 2);
13936bdc2ffdSmacallan						if (p->mskformat == PICT_a8) {
13946bdc2ffdSmacallan							msk = p->mskoff +
13956bdc2ffdSmacallan							    (maskY * p->mskpitch) +
13966bdc2ffdSmacallan							    maskX;
13976bdc2ffdSmacallan							CG14Comp_Over32Mask_noalpha(p,
13986bdc2ffdSmacallan							    src, p->srcpitch,
13996bdc2ffdSmacallan							    msk, p->mskpitch,
1400fa158432Smacallan							    dst, dstpitch,
1401e311bbeeSmacallan							    width, height, flip);
1402fa158432Smacallan						} else if ((p->mskformat == PICT_a8r8g8b8) ||
1403fa158432Smacallan							   (p->mskformat == PICT_a8b8g8r8)) {
1404fa158432Smacallan							msk = p->mskoff +
1405fa158432Smacallan							    (maskY * p->mskpitch) +
1406fa158432Smacallan							    (maskX << 2);
1407fa158432Smacallan							CG14Comp_Over32Mask32_noalpha(p,
1408fa158432Smacallan							    src, p->srcpitch,
1409fa158432Smacallan							    msk, p->mskpitch,
14106bdc2ffdSmacallan							    dst, dstpitch,
1411e311bbeeSmacallan							    width, height, flip);
14126bdc2ffdSmacallan						} else {
14136bdc2ffdSmacallan							xf86Msg(X_ERROR, "no src alpha, mask is %x\n", p->mskformat);
14146bdc2ffdSmacallan						}
1415a3a2ba44Smacallan						break;
1416a3a2ba44Smacallan					default:
1417a3a2ba44Smacallan						xf86Msg(X_ERROR, "%s: format %x in non-solid Over op\n",
1418a3a2ba44Smacallan						    __func__, p->srcformat);
1419a3a2ba44Smacallan				}
1420a3a2ba44Smacallan			}
14214261fa58Smacallan			break;
14224261fa58Smacallan		case PictOpAdd:
14234261fa58Smacallan			DPRINTF(X_ERROR, "Add %08x %08x\n",
14244261fa58Smacallan			    p->srcformat, p->dstformat);
14254261fa58Smacallan			switch (p->srcformat) {
14264261fa58Smacallan				case PICT_a8:
14274261fa58Smacallan					src = p->srcoff +
14284261fa58Smacallan					    (srcY * p->srcpitch) + srcX;
1429d71cb32dSmacallan					if (p->dstformat == PICT_a8) {
1430d71cb32dSmacallan						dst = dstoff +
1431d71cb32dSmacallan						      (dstY * dstpitch) + dstX;
1432d71cb32dSmacallan						CG14Comp_Add8(p,
1433d71cb32dSmacallan						    src, p->srcpitch,
1434d71cb32dSmacallan						    dst, dstpitch,
1435d71cb32dSmacallan						    width, height);
1436d71cb32dSmacallan					} else {
1437d71cb32dSmacallan						dst = dstoff +
1438d71cb32dSmacallan						      (dstY * dstpitch) +
1439d71cb32dSmacallan						      (dstX << 2);
1440d71cb32dSmacallan						CG14Comp_Add8_32(p,
1441d71cb32dSmacallan						    src, p->srcpitch,
1442d71cb32dSmacallan						    dst, dstpitch,
1443d71cb32dSmacallan						    width, height);
1444d71cb32dSmacallan					}
14454261fa58Smacallan					break;
14464261fa58Smacallan				case PICT_a8r8g8b8:
14474261fa58Smacallan				case PICT_x8r8g8b8:
14484261fa58Smacallan					src = p->srcoff +
14494261fa58Smacallan					    (srcY * p->srcpitch) + (srcX << 2);
14504261fa58Smacallan					dst = dstoff + (dstY * dstpitch) +
14514261fa58Smacallan					    (dstX << 2);
14524261fa58Smacallan					CG14Comp_Add32(p, src, p->srcpitch,
14534261fa58Smacallan					    dst, dstpitch, width, height);
14544261fa58Smacallan					break;
14554261fa58Smacallan				default:
14564261fa58Smacallan					xf86Msg(X_ERROR,
14574261fa58Smacallan					    "unsupported src format\n");
14584261fa58Smacallan			}
14594261fa58Smacallan			break;
1460a3a2ba44Smacallan		case PictOpSrc:
1461a3a2ba44Smacallan			DPRINTF(X_ERROR, "Src %08x %08x\n",
1462a3a2ba44Smacallan			    p->srcformat, p->dstformat);
1463239808baSmacallan			if (p->mskformat != 0)
1464239808baSmacallan				xf86Msg(X_ERROR, "Src mask %08x\n", p->mskformat);
1465f71acd79Smacallan			if (p->srcformat == PICT_a8) {
1466f71acd79Smacallan				CG14Copy8(pDst, srcX, srcY, dstX, dstY, width, height);
1467f71acd79Smacallan			} else {
1468f71acd79Smacallan				/* convert between RGB and BGR? */
1469f71acd79Smacallan				CG14Copy32(pDst, srcX, srcY, dstX, dstY, width, height);
1470f71acd79Smacallan			}
1471a3a2ba44Smacallan			break;
14724261fa58Smacallan		default:
14734261fa58Smacallan			xf86Msg(X_ERROR, "unsupported op %d\n", p->op);
14744261fa58Smacallan	}
14754261fa58Smacallan	exaMarkSync(pDst->drawable.pScreen);
14764261fa58Smacallan}
14774261fa58Smacallan
14784261fa58Smacallan
14794261fa58Smacallan
14804261fa58SmacallanBool
14814261fa58SmacallanCG14InitAccel(ScreenPtr pScreen)
14824261fa58Smacallan{
14834261fa58Smacallan	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
14844261fa58Smacallan	Cg14Ptr p = GET_CG14_FROM_SCRN(pScrn);
14854261fa58Smacallan	ExaDriverPtr pExa;
14864261fa58Smacallan
14874261fa58Smacallan	pExa = exaDriverAlloc();
14884261fa58Smacallan	if (!pExa)
14894261fa58Smacallan		return FALSE;
14904261fa58Smacallan
14914261fa58Smacallan	p->pExa = pExa;
14924261fa58Smacallan
14934261fa58Smacallan	pExa->exa_major = EXA_VERSION_MAJOR;
14944261fa58Smacallan	pExa->exa_minor = EXA_VERSION_MINOR;
14954261fa58Smacallan
14964261fa58Smacallan	pExa->memoryBase = p->fb;
14974261fa58Smacallan	pExa->memorySize = p->memsize;
1498b8ad197aSmacallan	pExa->offScreenBase = p->width * p->height * (pScrn->depth >> 3);
14994261fa58Smacallan
15004261fa58Smacallan	/*
15014261fa58Smacallan	 * SX memory instructions are written to 64bit aligned addresses with
15024261fa58Smacallan	 * a 3 bit displacement. Make sure the displacement remains constant
15034261fa58Smacallan	 * within one column
15044261fa58Smacallan	 */
15054261fa58Smacallan
15064261fa58Smacallan	pExa->pixmapOffsetAlign = 8;
15074261fa58Smacallan	pExa->pixmapPitchAlign = 8;
15084261fa58Smacallan
1509fe97f391Smacallan	pExa->flags = EXA_OFFSCREEN_PIXMAPS
1510f71acd79Smacallan		      | EXA_SUPPORTS_OFFSCREEN_OVERLAPS
1511f71acd79Smacallan		      /*| EXA_MIXED_PIXMAPS*/;
15124261fa58Smacallan
15134261fa58Smacallan	/*
15144261fa58Smacallan	 * these limits are bogus
15154261fa58Smacallan	 * SX doesn't deal with coordinates at all, so there is no limit but
15164261fa58Smacallan	 * we have to put something here
15174261fa58Smacallan	 */
15184261fa58Smacallan	pExa->maxX = 4096;
15194261fa58Smacallan	pExa->maxY = 4096;
15204261fa58Smacallan
15214261fa58Smacallan	pExa->WaitMarker = CG14WaitMarker;
15224261fa58Smacallan
15234261fa58Smacallan	pExa->PrepareSolid = CG14PrepareSolid;
15244261fa58Smacallan	pExa->Solid = CG14Solid;
15254261fa58Smacallan	pExa->DoneSolid = CG14DoneCopy;
15264261fa58Smacallan	pExa->PrepareCopy = CG14PrepareCopy;
1527f71acd79Smacallan	pExa->Copy = CG14Copy32;
15284261fa58Smacallan	pExa->DoneCopy = CG14DoneCopy;
15294261fa58Smacallan	if (p->use_xrender) {
15304261fa58Smacallan		pExa->CheckComposite = CG14CheckComposite;
15314261fa58Smacallan		pExa->PrepareComposite = CG14PrepareComposite;
15324261fa58Smacallan		pExa->Composite = CG14Composite;
15334261fa58Smacallan		pExa->DoneComposite = CG14DoneCopy;
15344261fa58Smacallan	}
15354261fa58Smacallan
15364261fa58Smacallan	/* EXA hits more optimized paths when it does not have to fallback
15374261fa58Smacallan	 * because of missing UTS/DFS, hook memcpy-based UTS/DFS.
15384261fa58Smacallan	 */
15394261fa58Smacallan	pExa->UploadToScreen = CG14UploadToScreen;
15404261fa58Smacallan	pExa->DownloadFromScreen = CG14DownloadFromScreen;
15414261fa58Smacallan
1542c2193d98Smacallan	p->queuecount = 0;
15434261fa58Smacallan	/* do some hardware init */
15444261fa58Smacallan	write_sx_reg(p, SX_PLANEMASK, 0xffffffff);
15454261fa58Smacallan	p->last_mask = 0xffffffff;
15464261fa58Smacallan	write_sx_reg(p, SX_ROP_CONTROL, 0xcc);
15474261fa58Smacallan	p->last_rop = 0xcc;
15484261fa58Smacallan	return exaDriverInit(pScreen, pExa);
15494261fa58Smacallan}
1550