ngle_accel.c revision bc460d0d
1/*
2 * NGLE - hardware acceleration.
3 *
4 * Copyright (C) 2024 Michael Lorenz
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * MICHAEL LORENZ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24/* $NetBSD: ngle_accel.c,v 1.2 2024/10/22 07:42:15 macallan Exp $ */
25
26#include <sys/types.h>
27#include <dev/ic/stireg.h>
28
29
30#include "ngle.h"
31
32//#define DEBUG
33
34#ifdef DEBUG
35#define ENTER xf86Msg(X_ERROR, "%s\n", __func__)
36#define LEAVE xf86Msg(X_ERROR, "%s done\n", __func__)
37#define DBGMSG xf86Msg
38#else
39#define ENTER
40#define DBGMSG if (0) xf86Msg
41#define LEAVE
42#endif
43
44static inline void
45NGLEWrite4(NGLEPtr fPtr, int offset, uint32_t val)
46{
47	volatile uint32_t *ptr = (uint32_t *)((uint8_t *)fPtr->regs + offset);
48	*ptr = val;
49}
50
51static inline void
52NGLEWrite1(NGLEPtr fPtr, int offset, uint8_t val)
53{
54	volatile uint8_t *ptr = (uint8_t *)fPtr->regs + offset;
55	*ptr = val;
56}
57
58static inline uint32_t
59NGLERead4(NGLEPtr fPtr, int offset)
60{
61	volatile uint32_t *ptr = (uint32_t *)((uint8_t *)fPtr->regs + offset);
62	return *ptr;
63}
64
65static inline uint8_t
66NGLERead1(NGLEPtr fPtr, int offset)
67{
68	volatile uint8_t *ptr = (uint8_t *)fPtr->regs + offset;
69	return *ptr;
70}
71
72static void
73NGLEWaitMarker(ScreenPtr pScreen, int Marker)
74{
75	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
76	NGLEPtr fPtr = NGLEPTR(pScrn);
77	uint8_t stat;
78
79	ENTER;
80	do {
81		stat = NGLERead1(fPtr, NGLE_REG_15b0);
82		if (stat == 0)
83			stat = NGLERead1(fPtr, NGLE_REG_15b0);
84	} while (stat != 0);
85	LEAVE;
86}
87
88static void
89NGLEWaitFifo(NGLEPtr fPtr, int slots)
90{
91	uint32_t reg;
92
93	ENTER;
94	do {
95		reg = NGLERead4(fPtr, NGLE_REG_34);
96	} while (reg < slots);
97	LEAVE;
98}
99
100static Bool
101NGLEPrepareCopy
102(
103    PixmapPtr pSrcPixmap,
104    PixmapPtr pDstPixmap,
105    int       xdir,
106    int       ydir,
107    int       alu,
108    Pixel     planemask
109)
110{
111	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
112	NGLEPtr fPtr = NGLEPTR(pScrn);
113	int srcpitch = exaGetPixmapPitch(pSrcPixmap);
114	int srcoff = exaGetPixmapOffset(pSrcPixmap);
115
116	ENTER;
117
118	DBGMSG(X_ERROR, "%s %d %d\n", __func__, srcoff, srcpitch);
119	fPtr->offset = srcoff / srcpitch;
120	NGLEWaitMarker(pDstPixmap->drawable.pScreen, 0);
121	/* XXX HCRX needs ifferent values here */
122	NGLEWrite4(fPtr, NGLE_REG_10,
123	    BA(IndexedDcd, Otc04, Ots08, AddrLong, 0, BINapp0I, 0));
124	NGLEWrite4(fPtr, NGLE_REG_14, ((alu << 8) & 0xf00) | 0x23000000);
125	NGLEWrite4(fPtr, NGLE_REG_13, planemask);
126
127	fPtr->hwmode = HW_BLIT;
128
129	LEAVE;
130	return TRUE;
131}
132
133static void
134NGLECopy
135(
136    PixmapPtr pDstPixmap,
137    int       xs,
138    int       ys,
139    int       xd,
140    int       yd,
141    int       wi,
142    int       he
143)
144{
145	ScrnInfoPtr pScrn = xf86Screens[pDstPixmap->drawable.pScreen->myNum];
146	NGLEPtr fPtr = NGLEPTR(pScrn);
147	int dstpitch = exaGetPixmapPitch(pDstPixmap);
148	int dstoff = exaGetPixmapOffset(pDstPixmap);
149
150	ENTER;
151	NGLEWaitFifo(fPtr, 3);
152	NGLEWrite4(fPtr, NGLE_REG_24, (xs << 16) | (ys + fPtr->offset));
153	NGLEWrite4(fPtr, NGLE_REG_7, (wi << 16) | he);
154	NGLEWrite4(fPtr, NGLE_REG_25, (xd << 16) | (yd + (dstoff / dstpitch)));
155
156	exaMarkSync(pDstPixmap->drawable.pScreen);
157	LEAVE;
158}
159
160static void
161NGLEDoneCopy(PixmapPtr pDstPixmap)
162{
163    ENTER;
164    LEAVE;
165}
166
167static Bool
168NGLEPrepareSolid(
169    PixmapPtr pPixmap,
170    int alu,
171    Pixel planemask,
172    Pixel fg)
173{
174	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
175	NGLEPtr fPtr = NGLEPTR(pScrn);
176
177	ENTER;
178	NGLEWaitFifo(fPtr, 4);
179	/* plane mask */
180	NGLEWrite4(fPtr, NGLE_REG_13, planemask);
181	/* bitmap op */
182	NGLEWrite4(fPtr, NGLE_REG_14,
183	    IBOvals(alu, 0, BitmapExtent08, 0, DataDynamic, MaskOtc, 1, 0));
184
185	/* XXX HCRX needs different values here */
186	/* dst bitmap access */
187	NGLEWrite4(fPtr, NGLE_REG_11,
188	    BA(IndexedDcd, Otc32, OtsIndirect, AddrLong, 0, BINapp0I, 0));
189    	NGLEWrite4(fPtr, NGLE_REG_35, fg);
190	fPtr->hwmode = HW_FILL;
191
192	LEAVE;
193	return TRUE;
194}
195
196static void
197NGLESolid(
198    PixmapPtr pPixmap,
199    int x1,
200    int y1,
201    int x2,
202    int y2)
203{
204	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
205	NGLEPtr fPtr = NGLEPTR(pScrn);
206	int w = x2 - x1, h = y2 - y1;
207	int pitch = exaGetPixmapPitch(pPixmap);
208	int offset = exaGetPixmapOffset(pPixmap);
209	uint32_t mask;
210	int wi, rest;
211
212	ENTER;
213
214	y1 += offset >> 11;
215
216	/*
217	 * XXX
218	 * Turns out this thing always fills rectangles to the next 32 pixel
219	 * boundary on te right. To get around this we split the rectangle
220	 * into a multiples-of-32 part and the rest, so we can mask off the
221	 * excess pixels.
222	 */
223	rest = w & 0x1f;
224	wi = w & 0xffffe0;
225	if (wi > 0) {
226		NGLEWaitFifo(fPtr, 3);
227		/* transfer data */
228		NGLEWrite4(fPtr, NGLE_REG_8, 0xffffffff);
229		/* dst XY */
230		NGLEWrite4(fPtr, NGLE_REG_6, (x1 << 16) | y1);
231		/* len XY start */
232		NGLEWrite4(fPtr, NGLE_REG_9, (wi << 16) | h);
233	}
234	if (rest > 0) {
235		mask = 0xffffffff << (32 - w);
236		/* transfer data */
237		NGLEWaitFifo(fPtr, 3);
238		NGLEWrite4(fPtr, NGLE_REG_8, mask);
239		/* dst XY */
240		NGLEWrite4(fPtr, NGLE_REG_6, ((x1 + wi) << 16) | y1);
241		/* len XY start */
242		NGLEWrite4(fPtr, NGLE_REG_9, (rest << 16) | h);
243	}
244	exaMarkSync(pPixmap->drawable.pScreen);
245	LEAVE;
246}
247
248Bool
249NGLEPrepareAccess(PixmapPtr pPixmap, int index)
250{
251	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
252	NGLEPtr fPtr = NGLEPTR(pScrn);
253
254	if (fPtr->hwmode == HW_FB) return TRUE;
255
256	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
257	NGLEWrite4(fPtr, NGLE_REG_10, fPtr->fbacc);
258	NGLEWrite4(fPtr, NGLE_REG_14, 0x83000300);
259	NGLEWrite4(fPtr, NGLE_REG_13, 0xff);
260	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
261	NGLEWrite1(fPtr, NGLE_REG_16b1, 1);
262	fPtr->hwmode = HW_FB;
263	return TRUE;
264}
265
266Bool
267NGLEInitAccel(ScreenPtr pScreen)
268{
269	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
270	NGLEPtr fPtr = NGLEPTR(pScrn);
271	ExaDriverPtr pExa;
272	int lines, bpp = pScrn->bitsPerPixel >> 3;
273
274	pExa = exaDriverAlloc();
275	if (!pExa)
276		return FALSE;
277
278	fPtr->pExa = pExa;
279
280	pExa->exa_major = EXA_VERSION_MAJOR;
281	pExa->exa_minor = EXA_VERSION_MINOR;
282
283	pExa->memoryBase = fPtr->fbmem;
284	lines = fPtr->fbmem_len / fPtr->fbi.fbi_stride;
285	DBGMSG(X_ERROR, "lines %d\n", lines);
286	pExa->memorySize = fPtr->fbmem_len;
287	pExa->offScreenBase = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
288	pExa->pixmapOffsetAlign = fPtr->fbi.fbi_stride;
289	pExa->pixmapPitchAlign = fPtr->fbi.fbi_stride;
290
291	pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_MIXED_PIXMAPS;
292
293	pExa->maxX = 2048;
294	pExa->maxY = 2048;
295
296	fPtr->hwmode = -1;
297
298	pExa->WaitMarker = NGLEWaitMarker;
299	pExa->PrepareSolid = NGLEPrepareSolid;
300	pExa->Solid = NGLESolid;
301	pExa->DoneSolid = NGLEDoneCopy;
302	pExa->PrepareCopy = NGLEPrepareCopy;
303	pExa->Copy = NGLECopy;
304	pExa->DoneCopy = NGLEDoneCopy;
305	pExa->PrepareAccess = NGLEPrepareAccess;
306	NGLEWaitMarker(pScreen, 0);
307
308	return exaDriverInit(pScreen, pExa);
309}
310