summaryrefslogtreecommitdiffstats
path: root/src/vivante_utils.c
blob: 8a6113351d0d4ee886dda7af841b56b2ad5a583d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Vivante GPU Acceleration Xorg driver
 *
 * Written by Russell King, 2012, derived in part from the
 * Intel xorg X server driver.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>

#ifdef HAVE_DIX_CONFIG_H
#include "dix-config.h"
#endif
#include "fb.h"
#include "gcstruct.h"
#include "xf86.h"

#include <armada_bufmgr.h>
#include "gal_extension.h"

#include "vivante_accel.h"
#include "vivante_utils.h"

static const char *vivante_errors[] = {
	"invalid argument",
	"invalid object",
	"out of memory",
	"memory locked",
	"memory unlocked",
	"heap corrupted",
	"generic IO",
	"invalid address",
	"context loss",
	"too complex",
	"buffer too small",
	"interface error",
	"not supported",
	"more data",
	"timeout",
	"out of resources",
	"invalid data",
	"invalid mipmap",
	"not found",
	"not aligned",
	"invalid request",
	"GPU unresponsive",
};

const char *vivante_strerror(int err)
{
	const char *str = NULL;

	if (err < 0) {
		if (err >= gcvSTATUS_GPU_NOT_RESPONDING)
			str = vivante_errors[-1 - err];
	}
	return str;
}

void __vivante_error(struct vivante *vivante, const char *fn, const char *w, int err)
{
	xf86DrvMsg(vivante->scrnIndex, X_ERROR,
		   "[vivante] %s: %s failed: %s\n", fn, w,
		   vivante_strerror(err));
}


static gceSTATUS vivante_ioctl(struct vivante *vivante, unsigned cmd,
	void *buf, size_t size)
{
	return gcoOS_DeviceControl(vivante->os, cmd, buf, size, buf, size);
}


PixmapPtr vivante_drawable_pixmap_deltas(DrawablePtr pDrawable, int *x, int *y)
{
	PixmapPtr pPixmap;

	*x = *y = 0;
	if (OnScreenDrawable(pDrawable->type)) {
		WindowPtr pWin = container_of(pDrawable, struct _Window, drawable);

		pPixmap = pDrawable->pScreen->GetWindowPixmap(pWin);
#ifdef COMPOSITE
		*x = -pPixmap->screen_x;
		*y = -pPixmap->screen_y;
#endif
	} else {
		pPixmap = container_of(pDrawable, struct _Pixmap, drawable);
	}
	return pPixmap;
}


/*
 * Unmap a pixmap from the GPU.  Note that we must wait for any outstanding
 * GPU operations to complete before unmapping the pixmap from the GPU.
 */
void vivante_unmap_gpu(struct vivante *vivante, struct vivante_pixmap *vPix)
{
	struct drm_armada_bo *bo = vPix->bo;
	gceSTATUS err;

#ifdef DEBUG_MAP
	dbg("Unmapping vPix %p bo %p\n", vPix, bo);
#endif
	err = gcoOS_UnmapUserMemory(vivante->os, bo->ptr, bo->size,
				    vPix->info, vPix->handle);
	if (err != gcvSTATUS_OK)
		vivante_error(vivante, "gcoOS_UnmapUserMemory", err);

	vPix->handle = -1;
	vPix->info = NULL;
}


Bool vivante_map_bo_to_gpu(struct vivante *vivante, struct drm_armada_bo *bo,
	void **info, uint32_t *handle)
{
	struct dmabuf_map map;
	gceSTATUS status;
	int fd;

	if (drm_armada_bo_to_fd(bo, &fd)) {
		xf86DrvMsg(vivante->scrnIndex, X_ERROR,
			   "vivante: unable to get prime fd for bo: %s\n",
			   strerror(errno));
		return FALSE;
	}

	map.hdr.v2.zero = 0;
	map.hdr.v2.status = 0;
	map.fd = fd;
	map.prot = PROT_READ | PROT_WRITE;

	status = vivante_ioctl(vivante, IOC_GDMABUF_MAP, &map, sizeof(map));

	/* we don't need to keep the fd around anymore */
	close(fd);

	if (gcmIS_ERROR(status)) {
		xf86DrvMsg(vivante->scrnIndex, X_INFO,
			   "vivante: gpu dmabuf map failed: %d\n",
			   status);
		return FALSE;
	}

	*handle = map.address;
	*info = (void *)(uintptr_t)map.info;

	return TRUE;
}

void vivante_unmap_from_gpu(struct vivante *vivante, void *info,
	uint32_t handle)
{
	gcoOS_UnmapUserMemory(vivante->os, (void *)1, 1, info, handle);
}

/*
 * Map a pixmap to the GPU, and mark the GPU as owning this BO.
 */
Bool vivante_map_gpu(struct vivante *vivante, struct vivante_pixmap *vPix)
{
	struct drm_armada_bo *bo = vPix->bo;

#ifdef DEBUG_CHECK_DRAWABLE_USE
	assert(vPix->in_use == 0);
#endif

	if (vPix->owner == GPU)
		return TRUE;

	if (bo->type == DRM_ARMADA_BO_SHMEM) {
		gceSTATUS err;
		gctUINT32 addr;

		err = gcoOS_MapUserMemory(vivante->os, bo->ptr, bo->size,
					  &vPix->info, &addr);
		if (err != gcvSTATUS_OK) {
			vivante_error(vivante, "gcoOS_MapUserMemory", err);
			return FALSE;
		}

#ifdef DEBUG_MAP
		dbg("Mapped vPix %p bo %p to 0x%08x\n", vPix, bo, addr);
#endif

		vPix->handle = addr;
	}
	vPix->owner = GPU;

	return TRUE;
}

/*
 * Finish a bo for CPU access.  NULL out the fb layer's pixmap data
 * pointer to ensure any further unprotected accesses get caught.
 */
void vivante_finish_drawable(DrawablePtr pDrawable, int access)
{
	PixmapPtr pixmap = vivante_drawable_pixmap(pDrawable);
	struct vivante_pixmap *vPix = vivante_get_pixmap_priv(pixmap);

	if (vPix) {
#ifdef DEBUG_CHECK_DRAWABLE_USE
		vPix->in_use--;
#endif
		if (vPix->bo->type == DRM_ARMADA_BO_SHMEM)
			pixmap->devPrivate.ptr = NULL;
	}
}

/*
 * Prepare a bo for CPU access.  If the GPU has been accessing the
 * pixmap data, we need to unmap the buffer from the GPU to ensure
 * that our view is up to date.
 */
void vivante_prepare_drawable(DrawablePtr pDrawable, int access)
{
	PixmapPtr pixmap = vivante_drawable_pixmap(pDrawable);
	struct vivante_pixmap *vPix = vivante_get_pixmap_priv(pixmap);

	if (vPix) {
		struct vivante *vivante = vivante_get_screen_priv(pDrawable->pScreen);

		/* Ensure that the drawable is up to date with all GPU operations */
		vivante_batch_wait_commit(vivante, vPix);

		if (vPix->bo->type == DRM_ARMADA_BO_SHMEM) {
			if (vPix->owner == GPU)
				vivante_unmap_gpu(vivante, vPix);

			pixmap->devPrivate.ptr = vPix->bo->ptr;
		}
#ifdef DEBUG_CHECK_DRAWABLE_USE
		vPix->in_use++;
#endif
		vPix->owner = CPU;
	}
}

#ifdef RENDER
gceSURF_FORMAT vivante_pict_format(PictFormatShort format, Bool force)
{
	switch (format) {
#define C(pf,vf,af) case PICT_##pf: return force ? gcvSURF_##af : gcvSURF_##vf
	C(a2r10g10b10,	A2R10G10B10,	A2R10G10B10);
	C(x2r10g10b10,	X2R10G10B10,	A2R10G10B10);
	C(a2b10g10r10,	A2B10G10R10,	A2B10G10R10);
	C(x2b10g10r10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OWN);
	C(c4,		INDEX4,		UNKNOWN);
	C(g4,		L4,		UNKNOWN);
	C(a1,		A1,		A1);
	C(g1,		L1,		UNKNOWN);
#undef C
	}
	return gcvSURF_UNKNOWN;
}
#endif


#if 1 //def DEBUG
#include <stdarg.h>
#include <sys/fcntl.h>
#include <unistd.h>
static void dump_pix(struct vivante *vivante, struct vivante_pixmap *vPix,
	int x1, int y1, int x2, int y2, int alpha,
	const char *fmt, va_list ap)
{
	struct drm_armada_bo *bo = vPix->bo;
	static int idx;
	unsigned owner = vPix->owner;
	char fn[160], n[80];
	int fd;

	if (vPix->bo->type != DRM_ARMADA_BO_SHMEM)
		owner = CPU;

	if (owner == GPU) {
		vivante_unmap_gpu(vivante, vPix);
		vPix->owner = CPU;
	}

	vsprintf(n, fmt, ap);
	sprintf(fn, "/tmp/X.%04u.%s-%u.%u.%u.%u.pam",
			idx++, n, x1, y1, x2, y2);
	fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (fd >= 0) {
		char buf[16*1024 + 16384];
		uint32_t *bo_p;
		int x, y, i;

		sprintf(buf, "P7\nWIDTH %u\nHEIGHT %u\nDEPTH %u\nMAXVAL 255\nTUPLTYPE RGB%s\nENDHDR\n",
				x2 - x1, y2 - y1, 3 + alpha, alpha ? "_ALPHA" : "");
		write(fd, buf, strlen(buf));

		for (y = y1, i = 0; y < y2; y++) {
			bo_p = (((void *)bo->ptr) + (y * vPix->pitch));
			for (x = x1; x < x2; x++) {
				buf[i++] = bo_p[x] >> 16; // R
				buf[i++] = bo_p[x] >> 8;  // G
				buf[i++] = bo_p[x];       // B
				if (alpha)
					buf[i++] = bo_p[x] >> 24; // A
			}
			if (i >= 16*1024) {
				write(fd, buf, i);
				i = 0;
			}
		}
		if (i)
			write(fd, buf, i);
		close(fd);
	}

	if (owner == GPU)
		vivante_map_gpu(vivante, vPix);
}

void dump_Drawable(DrawablePtr pDraw, const char *fmt, ...)
{
	struct vivante *vivante = vivante_get_screen_priv(pDraw->pScreen);
	struct vivante_pixmap *vPix;
	PixmapPtr pPix;
	int off_x, off_y;
	va_list ap;

	pPix = vivante_drawable_pixmap_deltas(pDraw, &off_x, &off_y);
	vPix = vivante_get_pixmap_priv(pPix);

	if (!vPix)
		return;

	va_start(ap, fmt);
	dump_pix(vivante, vPix, 0, 0, pDraw->width, pDraw->height, 0, fmt, ap);
	va_end(ap);
}

void dump_Picture(PicturePtr pDst, const char *fmt, ...)
{
	struct vivante *vivante = vivante_get_screen_priv(pDst->pDrawable->pScreen);
	struct vivante_pixmap *vPix;
	PixmapPtr pPix;
	int alpha, off_x, off_y;
	va_list ap;

	pPix = vivante_drawable_pixmap_deltas(pDst->pDrawable, &off_x, &off_y);
	vPix = vivante_get_pixmap_priv(pPix);

	if (!vPix)
		return;

	alpha = PICT_FORMAT_A(pDst->format) != 0;

	va_start(ap, fmt);
	dump_pix(vivante, vPix, 0, 0, vPix->width, vPix->height, alpha, fmt, ap);
	va_end(ap);
}

void dump_vPix(struct vivante *vivante, struct vivante_pixmap *vPix,
	      int alpha, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	dump_pix(vivante, vPix, 0, 0, vPix->width, vPix->height, alpha, fmt, ap);
	va_end(ap);
}
#endif