ngle_accel.c revision 727c6de2
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.1 2024/10/21 13:40:53 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 ifferent 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	rest = w & 0x1f;
217	wi = w & 0xffffe0;
218	if (wi > 0) {
219		NGLEWaitFifo(fPtr, 3);
220		/* transfer data */
221		NGLEWrite4(fPtr, NGLE_REG_8, 0xffffffff);
222		/* dst XY */
223		NGLEWrite4(fPtr, NGLE_REG_6, (x1 << 16) | y1);
224		/* len XY start */
225		NGLEWrite4(fPtr, NGLE_REG_9, (wi << 16) | h);
226	}
227	if (rest > 0) {
228		mask = 0xffffffff << (32 - w);
229		/* transfer data */
230		NGLEWrite4(fPtr, NGLE_REG_8, mask);
231		/* dst XY */
232		NGLEWrite4(fPtr, NGLE_REG_6, ((x1 + wi) << 16) | y1);
233		/* len XY start */
234		NGLEWrite4(fPtr, NGLE_REG_9, (rest << 16) | h);
235	}
236	exaMarkSync(pPixmap->drawable.pScreen);
237	LEAVE;
238}
239
240Bool
241NGLEPrepareAccess(PixmapPtr pPixmap, int index)
242{
243	ScrnInfoPtr pScrn = xf86Screens[pPixmap->drawable.pScreen->myNum];
244	NGLEPtr fPtr = NGLEPTR(pScrn);
245
246	if (fPtr->hwmode == HW_FB) return TRUE;
247
248	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
249	NGLEWrite4(fPtr, NGLE_REG_10, fPtr->fbacc);
250	NGLEWrite4(fPtr, NGLE_REG_14, 0x83000300);
251	NGLEWrite4(fPtr, NGLE_REG_13, 0xff);
252	NGLEWaitMarker(pPixmap->drawable.pScreen, 0);
253	NGLEWrite1(fPtr, NGLE_REG_16b1, 1);
254	fPtr->hwmode = HW_FB;
255	return TRUE;
256}
257
258Bool
259NGLEInitAccel(ScreenPtr pScreen)
260{
261	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
262	NGLEPtr fPtr = NGLEPTR(pScrn);
263	ExaDriverPtr pExa;
264	int lines, bpp = pScrn->bitsPerPixel >> 3;
265
266	pExa = exaDriverAlloc();
267	if (!pExa)
268		return FALSE;
269
270	fPtr->pExa = pExa;
271
272	pExa->exa_major = EXA_VERSION_MAJOR;
273	pExa->exa_minor = EXA_VERSION_MINOR;
274
275	pExa->memoryBase = fPtr->fbmem;
276	lines = fPtr->fbmem_len / fPtr->fbi.fbi_stride;
277	DBGMSG(X_ERROR, "lines %d\n", lines);
278	pExa->memorySize = fPtr->fbmem_len;
279	pExa->offScreenBase = fPtr->fbi.fbi_stride * fPtr->fbi.fbi_height;
280	pExa->pixmapOffsetAlign = fPtr->fbi.fbi_stride;
281	pExa->pixmapPitchAlign = fPtr->fbi.fbi_stride;
282
283	pExa->flags = EXA_OFFSCREEN_PIXMAPS | EXA_MIXED_PIXMAPS;
284
285	pExa->maxX = 2048;
286	pExa->maxY = 2048;
287
288	fPtr->hwmode = -1;
289
290	pExa->WaitMarker = NGLEWaitMarker;
291	pExa->PrepareSolid = NGLEPrepareSolid;
292	pExa->Solid = NGLESolid;
293	pExa->DoneSolid = NGLEDoneCopy;
294	pExa->PrepareCopy = NGLEPrepareCopy;
295	pExa->Copy = NGLECopy;
296	pExa->DoneCopy = NGLEDoneCopy;
297	pExa->PrepareAccess = NGLEPrepareAccess;
298	NGLEWaitMarker(pScreen, 0);
299
300	return exaDriverInit(pScreen, pExa);
301}
302