ngle_accel.c revision bc460d0d
1727c6de2Smacallan/*
2727c6de2Smacallan * NGLE - hardware acceleration.
3727c6de2Smacallan *
4727c6de2Smacallan * Copyright (C) 2024 Michael Lorenz
5727c6de2Smacallan *
6727c6de2Smacallan * Permission is hereby granted, free of charge, to any person obtaining a copy
7727c6de2Smacallan * of this software and associated documentation files (the "Software"), to deal
8727c6de2Smacallan * in the Software without restriction, including without limitation the rights
9727c6de2Smacallan * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10727c6de2Smacallan * copies of the Software, and to permit persons to whom the Software is
11727c6de2Smacallan * furnished to do so, subject to the following conditions:
12727c6de2Smacallan *
13727c6de2Smacallan * The above copyright notice and this permission notice shall be included in
14727c6de2Smacallan * all copies or substantial portions of the Software.
15727c6de2Smacallan *
16727c6de2Smacallan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17727c6de2Smacallan * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18727c6de2Smacallan * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19727c6de2Smacallan * MICHAEL LORENZ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20727c6de2Smacallan * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21727c6de2Smacallan * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22727c6de2Smacallan */
23727c6de2Smacallan
24bc460d0dSmacallan/* $NetBSD: ngle_accel.c,v 1.2 2024/10/22 07:42:15 macallan Exp $ */
25727c6de2Smacallan
26727c6de2Smacallan#include <sys/types.h>
27727c6de2Smacallan#include <dev/ic/stireg.h>
28727c6de2Smacallan
29727c6de2Smacallan
30727c6de2Smacallan#include "ngle.h"
31727c6de2Smacallan
32727c6de2Smacallan//#define DEBUG
33727c6de2Smacallan
34727c6de2Smacallan#ifdef DEBUG
35727c6de2Smacallan#define ENTER xf86Msg(X_ERROR, "%s\n", __func__)
36727c6de2Smacallan#define LEAVE xf86Msg(X_ERROR, "%s done\n", __func__)
37727c6de2Smacallan#define DBGMSG xf86Msg
38727c6de2Smacallan#else
39727c6de2Smacallan#define ENTER
40727c6de2Smacallan#define DBGMSG if (0) xf86Msg
41727c6de2Smacallan#define LEAVE
42727c6de2Smacallan#endif
43727c6de2Smacallan
44727c6de2Smacallanstatic inline void
45727c6de2SmacallanNGLEWrite4(NGLEPtr fPtr, int offset, uint32_t val)
46727c6de2Smacallan{
47727c6de2Smacallan	volatile uint32_t *ptr = (uint32_t *)((uint8_t *)fPtr->regs + offset);
48727c6de2Smacallan	*ptr = val;
49727c6de2Smacallan}
50727c6de2Smacallan
51727c6de2Smacallanstatic inline void
52727c6de2SmacallanNGLEWrite1(NGLEPtr fPtr, int offset, uint8_t val)
53727c6de2Smacallan{
54727c6de2Smacallan	volatile uint8_t *ptr = (uint8_t *)fPtr->regs + offset;
55727c6de2Smacallan	*ptr = val;
56727c6de2Smacallan}
57727c6de2Smacallan
58727c6de2Smacallanstatic inline uint32_t
59727c6de2SmacallanNGLERead4(NGLEPtr fPtr, int offset)
60727c6de2Smacallan{
61727c6de2Smacallan	volatile uint32_t *ptr = (uint32_t *)((uint8_t *)fPtr->regs + offset);
62727c6de2Smacallan	return *ptr;
63727c6de2Smacallan}
64727c6de2Smacallan
65727c6de2Smacallanstatic inline uint8_t
66727c6de2SmacallanNGLERead1(NGLEPtr fPtr, int offset)
67727c6de2Smacallan{
68727c6de2Smacallan	volatile uint8_t *ptr = (uint8_t *)fPtr->regs + offset;
69727c6de2Smacallan	return *ptr;
70727c6de2Smacallan}
71727c6de2Smacallan
72727c6de2Smacallanstatic void
73727c6de2SmacallanNGLEWaitMarker(ScreenPtr pScreen, int Marker)
74727c6de2Smacallan{
75727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
76727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
77727c6de2Smacallan	uint8_t stat;
78727c6de2Smacallan
79727c6de2Smacallan	ENTER;
80727c6de2Smacallan	do {
81727c6de2Smacallan		stat = NGLERead1(fPtr, NGLE_REG_15b0);
82727c6de2Smacallan		if (stat == 0)
83727c6de2Smacallan			stat = NGLERead1(fPtr, NGLE_REG_15b0);
84727c6de2Smacallan	} while (stat != 0);
85727c6de2Smacallan	LEAVE;
86727c6de2Smacallan}
87727c6de2Smacallan
88727c6de2Smacallanstatic void
89727c6de2SmacallanNGLEWaitFifo(NGLEPtr fPtr, int slots)
90727c6de2Smacallan{
91727c6de2Smacallan	uint32_t reg;
92727c6de2Smacallan
93727c6de2Smacallan	ENTER;
94727c6de2Smacallan	do {
95727c6de2Smacallan		reg = NGLERead4(fPtr, NGLE_REG_34);
96727c6de2Smacallan	} while (reg < slots);
97727c6de2Smacallan	LEAVE;
98727c6de2Smacallan}
99727c6de2Smacallan
100727c6de2Smacallanstatic Bool
101727c6de2SmacallanNGLEPrepareCopy
102727c6de2Smacallan(
103727c6de2Smacallan    PixmapPtr pSrcPixmap,
104727c6de2Smacallan    PixmapPtr pDstPixmap,
105727c6de2Smacallan    int       xdir,
106727c6de2Smacallan    int       ydir,
107727c6de2Smacallan    int       alu,
108727c6de2Smacallan    Pixel     planemask
109727c6de2Smacallan)
110727c6de2Smacallan{
111727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
112727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
113727c6de2Smacallan	int srcpitch = exaGetPixmapPitch(pSrcPixmap);
114727c6de2Smacallan	int srcoff = exaGetPixmapOffset(pSrcPixmap);
115727c6de2Smacallan
116727c6de2Smacallan	ENTER;
117727c6de2Smacallan
118727c6de2Smacallan	DBGMSG(X_ERROR, "%s %d %d\n", __func__, srcoff, srcpitch);
119727c6de2Smacallan	fPtr->offset = srcoff / srcpitch;
120727c6de2Smacallan	NGLEWaitMarker(pDstPixmap->drawable.pScreen, 0);
121727c6de2Smacallan	/* XXX HCRX needs ifferent values here */
122727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_10,
123727c6de2Smacallan	    BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINapp0I, 0));
124727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_14, ((alu << 8) & 0xf00) | 0x23000000);
125727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_13, planemask);
126727c6de2Smacallan
127727c6de2Smacallan	fPtr->hwmode = HW_BLIT;
128727c6de2Smacallan
129727c6de2Smacallan	LEAVE;
130727c6de2Smacallan	return TRUE;
131727c6de2Smacallan}
132727c6de2Smacallan
133727c6de2Smacallanstatic void
134727c6de2SmacallanNGLECopy
135727c6de2Smacallan(
136727c6de2Smacallan    PixmapPtr pDstPixmap,
137727c6de2Smacallan    int       xs,
138727c6de2Smacallan    int       ys,
139727c6de2Smacallan    int       xd,
140727c6de2Smacallan    int       yd,
141727c6de2Smacallan    int       wi,
142727c6de2Smacallan    int       he
143727c6de2Smacallan)
144727c6de2Smacallan{
145727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
146727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
147727c6de2Smacallan	int dstpitch = exaGetPixmapPitch(pDstPixmap);
148727c6de2Smacallan	int dstoff = exaGetPixmapOffset(pDstPixmap);
149727c6de2Smacallan
150727c6de2Smacallan	ENTER;
151727c6de2Smacallan	NGLEWaitFifo(fPtr, 3);
152727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_24, (xs << 16) | (ys + fPtr->offset));
153727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_7, (wi << 16) | he);
154727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_25, (xd << 16) | (yd + (dstoff / dstpitch)));
155727c6de2Smacallan
156727c6de2Smacallan	exaMarkSync(pDstPixmap->drawable.pScreen);
157727c6de2Smacallan	LEAVE;
158727c6de2Smacallan}
159727c6de2Smacallan
160727c6de2Smacallanstatic void
161727c6de2SmacallanNGLEDoneCopy(PixmapPtr pDstPixmap)
162727c6de2Smacallan{
163727c6de2Smacallan    ENTER;
164727c6de2Smacallan    LEAVE;
165727c6de2Smacallan}
166727c6de2Smacallan
167727c6de2Smacallanstatic Bool
168727c6de2SmacallanNGLEPrepareSolid(
169727c6de2Smacallan    PixmapPtr pPixmap,
170727c6de2Smacallan    int alu,
171727c6de2Smacallan    Pixel planemask,
172727c6de2Smacallan    Pixel fg)
173727c6de2Smacallan{
174727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
175727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
176727c6de2Smacallan
177727c6de2Smacallan	ENTER;
178727c6de2Smacallan	NGLEWaitFifo(fPtr, 4);
179727c6de2Smacallan	/* plane mask */
180727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_13, planemask);
181727c6de2Smacallan	/* bitmap op */
182727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_14,
183727c6de2Smacallan	    IBOvals(alu, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 1, 0));
184727c6de2Smacallan
185bc460d0dSmacallan	/* XXX HCRX needs different values here */
186727c6de2Smacallan	/* dst bitmap access */
187727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_11,
188727c6de2Smacallan	    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
189727c6de2Smacallan    	NGLEWrite4(fPtr, NGLE_REG_35, fg);
190727c6de2Smacallan	fPtr->hwmode = HW_FILL;
191727c6de2Smacallan
192727c6de2Smacallan	LEAVE;
193727c6de2Smacallan	return TRUE;
194727c6de2Smacallan}
195727c6de2Smacallan
196727c6de2Smacallanstatic void
197727c6de2SmacallanNGLESolid(
198727c6de2Smacallan    PixmapPtr pPixmap,
199727c6de2Smacallan    int x1,
200727c6de2Smacallan    int y1,
201727c6de2Smacallan    int x2,
202727c6de2Smacallan    int y2)
203727c6de2Smacallan{
204727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
205727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
206727c6de2Smacallan	int w = x2 - x1, h = y2 - y1;
207727c6de2Smacallan	int pitch = exaGetPixmapPitch(pPixmap);
208727c6de2Smacallan	int offset = exaGetPixmapOffset(pPixmap);
209727c6de2Smacallan	uint32_t mask;
210727c6de2Smacallan	int wi, rest;
211727c6de2Smacallan
212727c6de2Smacallan	ENTER;
213727c6de2Smacallan
214727c6de2Smacallan	y1 += offset >> 11;
215727c6de2Smacallan
216bc460d0dSmacallan	/*
217bc460d0dSmacallan	 * XXX
218bc460d0dSmacallan	 * Turns out this thing always fills rectangles to the next 32 pixel
219bc460d0dSmacallan	 * boundary on te right. To get around this we split the rectangle
220bc460d0dSmacallan	 * into a multiples-of-32 part and the rest, so we can mask off the
221bc460d0dSmacallan	 * excess pixels.
222bc460d0dSmacallan	 */
223727c6de2Smacallan	rest = w & 0x1f;
224727c6de2Smacallan	wi = w & 0xffffe0;
225727c6de2Smacallan	if (wi > 0) {
226727c6de2Smacallan		NGLEWaitFifo(fPtr, 3);
227727c6de2Smacallan		/* transfer data */
228727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_8, 0xffffffff);
229727c6de2Smacallan		/* dst XY */
230727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_6, (x1 << 16) | y1);
231727c6de2Smacallan		/* len XY start */
232727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_9, (wi << 16) | h);
233727c6de2Smacallan	}
234727c6de2Smacallan	if (rest > 0) {
235727c6de2Smacallan		mask = 0xffffffff << (32 - w);
236727c6de2Smacallan		/* transfer data */
237bc460d0dSmacallan		NGLEWaitFifo(fPtr, 3);
238727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_8, mask);
239727c6de2Smacallan		/* dst XY */
240727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_6, ((x1 + wi) << 16) | y1);
241727c6de2Smacallan		/* len XY start */
242727c6de2Smacallan		NGLEWrite4(fPtr, NGLE_REG_9, (rest << 16) | h);
243727c6de2Smacallan	}
244727c6de2Smacallan	exaMarkSync(pPixmap->drawable.pScreen);
245727c6de2Smacallan	LEAVE;
246727c6de2Smacallan}
247727c6de2Smacallan
248727c6de2SmacallanBool
249727c6de2SmacallanNGLEPrepareAccess(PixmapPtr pPixmap, int index)
250727c6de2Smacallan{
251727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
252727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
253727c6de2Smacallan
254727c6de2Smacallan	if (fPtr->hwmode == HW_FB) return TRUE;
255727c6de2Smacallan
256727c6de2Smacallan	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
257727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_10, fPtr->fbacc);
258727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_14, 0x83000300);
259727c6de2Smacallan	NGLEWrite4(fPtr, NGLE_REG_13, 0xff);
260727c6de2Smacallan	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
261727c6de2Smacallan	NGLEWrite1(fPtr, NGLE_REG_16b1, 1);
262727c6de2Smacallan	fPtr->hwmode = HW_FB;
263727c6de2Smacallan	return TRUE;
264727c6de2Smacallan}
265727c6de2Smacallan
266727c6de2SmacallanBool
267727c6de2SmacallanNGLEInitAccel(ScreenPtr pScreen)
268727c6de2Smacallan{
269727c6de2Smacallan	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
270727c6de2Smacallan	NGLEPtr fPtr = NGLEPTR(pScrn);
271727c6de2Smacallan	ExaDriverPtr pExa;
272727c6de2Smacallan	int lines, bpp = pScrn->bitsPerPixel >> 3;
273727c6de2Smacallan
274727c6de2Smacallan	pExa = exaDriverAlloc();
275727c6de2Smacallan	if (!pExa)
276727c6de2Smacallan		return FALSE;
277727c6de2Smacallan
278727c6de2Smacallan	fPtr->pExa = pExa;
279727c6de2Smacallan
280727c6de2Smacallan	pExa->exa_major = EXA_VERSION_MAJOR;
281727c6de2Smacallan	pExa->exa_minor = EXA_VERSION_MINOR;
282727c6de2Smacallan
283727c6de2Smacallan	pExa->memoryBase = fPtr->fbmem;
284727c6de2Smacallan	lines = fPtr->fbmem_len / fPtr->fbi.fbi_stride;
285727c6de2Smacallan	DBGMSG(X_ERROR, "lines %d\n", lines);
286727c6de2Smacallan	pExa->memorySize = fPtr->fbmem_len;
287727c6de2Smacallan	pExa->offScreenBase = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
288727c6de2Smacallan	pExa->pixmapOffsetAlign = fPtr->fbi.fbi_stride;
289727c6de2Smacallan	pExa->pixmapPitchAlign = fPtr->fbi.fbi_stride;
290727c6de2Smacallan
291727c6de2Smacallan	pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_MIXED_PIXMAPS;
292727c6de2Smacallan
293727c6de2Smacallan	pExa->maxX = 2048;
294727c6de2Smacallan	pExa->maxY = 2048;
295727c6de2Smacallan
296727c6de2Smacallan	fPtr->hwmode = -1;
297727c6de2Smacallan
298727c6de2Smacallan	pExa->WaitMarker = NGLEWaitMarker;
299727c6de2Smacallan	pExa->PrepareSolid = NGLEPrepareSolid;
300727c6de2Smacallan	pExa->Solid = NGLESolid;
301727c6de2Smacallan	pExa->DoneSolid = NGLEDoneCopy;
302727c6de2Smacallan	pExa->PrepareCopy = NGLEPrepareCopy;
303727c6de2Smacallan	pExa->Copy = NGLECopy;
304727c6de2Smacallan	pExa->DoneCopy = NGLEDoneCopy;
305727c6de2Smacallan	pExa->PrepareAccess = NGLEPrepareAccess;
306727c6de2Smacallan	NGLEWaitMarker(pScreen, 0);
307727c6de2Smacallan
308727c6de2Smacallan	return exaDriverInit(pScreen, pExa);
309727c6de2Smacallan}
310