• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Advanced Micro Devices. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #ifdef DRV_AMDGPU
8 
9 #include <assert.h>
10 #include <dlfcn.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/mman.h>
17 #include <unistd.h>
18 #include <xf86drm.h>
19 
20 #include "dri.h"
21 #include "drv_priv.h"
22 #include "helpers.h"
23 #include "util.h"
24 
25 static const struct {
26 	uint32_t drm_format;
27 	int dri_image_format;
28 } drm_to_dri_image_formats[] = {
29 	{ DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8 },
30 	{ DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88 },
31 	{ DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565 },
32 	{ DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888 },
33 	{ DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888 },
34 	{ DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888 },
35 	{ DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888 },
36 	{ DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010 },
37 	{ DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
38 };
39 
drm_format_to_dri_format(uint32_t drm_format)40 static int drm_format_to_dri_format(uint32_t drm_format)
41 {
42 	uint32_t i;
43 	for (i = 0; i < ARRAY_SIZE(drm_to_dri_image_formats); i++) {
44 		if (drm_to_dri_image_formats[i].drm_format == drm_format)
45 			return drm_to_dri_image_formats[i].dri_image_format;
46 	}
47 
48 	return 0;
49 }
50 
lookup_extension(const __DRIextension * const * extensions,const char * name,int min_version,const __DRIextension ** dst)51 static bool lookup_extension(const __DRIextension *const *extensions, const char *name,
52 			     int min_version, const __DRIextension **dst)
53 {
54 	while (*extensions) {
55 		if ((*extensions)->name && !strcmp((*extensions)->name, name) &&
56 		    (*extensions)->version >= min_version) {
57 			*dst = *extensions;
58 			return true;
59 		}
60 
61 		extensions++;
62 	}
63 
64 	return false;
65 }
66 
67 /*
68  * Close Gem Handle
69  */
close_gem_handle(uint32_t handle,int fd)70 static void close_gem_handle(uint32_t handle, int fd)
71 {
72 	struct drm_gem_close gem_close;
73 	int ret = 0;
74 
75 	memset(&gem_close, 0, sizeof(gem_close));
76 	gem_close.handle = handle;
77 	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
78 	if (ret)
79 		drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", handle, ret);
80 }
81 
82 /*
83  * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
84  * to import into minigbm.
85  */
import_into_minigbm(struct dri_driver * dri,struct bo * bo)86 static int import_into_minigbm(struct dri_driver *dri, struct bo *bo)
87 {
88 	uint32_t handle;
89 	int ret, modifier_upper, modifier_lower, num_planes, i, j;
90 	off_t dmabuf_sizes[DRV_MAX_PLANES];
91 	__DRIimage *plane_image = NULL;
92 
93 	if (dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
94 					     &modifier_upper) &&
95 	    dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
96 					     &modifier_lower)) {
97 		bo->meta.format_modifiers[0] =
98 		    ((uint64_t)modifier_upper << 32) | (uint32_t)modifier_lower;
99 	} else {
100 		bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_INVALID;
101 	}
102 
103 	if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_NUM_PLANES,
104 					      &num_planes)) {
105 		return -errno;
106 	}
107 
108 	bo->meta.num_planes = num_planes;
109 
110 	for (i = 0; i < num_planes; ++i) {
111 		int prime_fd, stride, offset;
112 		plane_image = dri->image_extension->fromPlanar(bo->priv, i, NULL);
113 		__DRIimage *image = plane_image ? plane_image : bo->priv;
114 
115 		if (i)
116 			bo->meta.format_modifiers[i] = bo->meta.format_modifiers[0];
117 
118 		if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride) ||
119 		    !dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
120 			ret = -errno;
121 			goto cleanup;
122 		}
123 
124 		if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &prime_fd)) {
125 			ret = -errno;
126 			goto cleanup;
127 		}
128 
129 		dmabuf_sizes[i] = lseek(prime_fd, 0, SEEK_END);
130 		if (dmabuf_sizes[i] == (off_t)-1) {
131 			ret = -errno;
132 			close(prime_fd);
133 			goto cleanup;
134 		}
135 
136 		lseek(prime_fd, 0, SEEK_SET);
137 
138 		ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
139 
140 		close(prime_fd);
141 
142 		if (ret) {
143 			drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
144 			goto cleanup;
145 		}
146 
147 		bo->handles[i].u32 = handle;
148 
149 		bo->meta.strides[i] = stride;
150 		bo->meta.offsets[i] = offset;
151 
152 		if (plane_image)
153 			dri->image_extension->destroyImage(plane_image);
154 	}
155 
156 	for (i = 0; i < num_planes; ++i) {
157 		off_t next_plane = dmabuf_sizes[i];
158 		for (j = 0; j < num_planes; ++j) {
159 			if (bo->meta.offsets[j] < next_plane &&
160 			    bo->meta.offsets[j] > bo->meta.offsets[i] &&
161 			    bo->handles[j].u32 == bo->handles[i].u32)
162 				next_plane = bo->meta.offsets[j];
163 		}
164 
165 		bo->meta.sizes[i] = next_plane - bo->meta.offsets[i];
166 
167 		/* This is kind of misleading if different planes use
168 		   different dmabufs. */
169 		bo->meta.total_size += bo->meta.sizes[i];
170 	}
171 
172 	return 0;
173 
174 cleanup:
175 	if (plane_image)
176 		dri->image_extension->destroyImage(plane_image);
177 	while (--i >= 0) {
178 		for (j = 0; j <= i; ++j)
179 			if (bo->handles[j].u32 == bo->handles[i].u32)
180 				break;
181 
182 		/* Multiple equivalent handles) */
183 		if (i == j)
184 			break;
185 
186 		/* This kind of goes horribly wrong when we already imported
187 		 * the same handles earlier, as we should really reference
188 		 * count handles. */
189 		close_gem_handle(bo->handles[i].u32, bo->drv->fd);
190 	}
191 	return ret;
192 }
193 
194 /*
195  * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
196  */
dri_init(struct driver * drv,const char * dri_so_path,const char * driver_suffix)197 int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
198 {
199 	char fname[128];
200 	const __DRIextension **(*get_extensions)();
201 	const __DRIextension *loader_extensions[] = { NULL };
202 
203 	struct dri_driver *dri = drv->priv;
204 
205 	dri->fd = open(drmGetRenderDeviceNameFromFd(drv_get_fd(drv)), O_RDWR);
206 	if (dri->fd < 0)
207 		return -ENODEV;
208 
209 	dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
210 	if (!dri->driver_handle)
211 		goto close_dri_fd;
212 
213 	snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
214 	get_extensions = dlsym(dri->driver_handle, fname);
215 	if (!get_extensions)
216 		goto free_handle;
217 
218 	dri->extensions = get_extensions();
219 	if (!dri->extensions)
220 		goto free_handle;
221 
222 	if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
223 			      (const __DRIextension **)&dri->core_extension))
224 		goto free_handle;
225 
226 	/* Version 4 for createNewScreen2 */
227 	if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
228 			      (const __DRIextension **)&dri->dri2_extension))
229 		goto free_handle;
230 
231 	dri->device = dri->dri2_extension->createNewScreen2(0, dri->fd, loader_extensions,
232 							    dri->extensions, &dri->configs, NULL);
233 	if (!dri->device)
234 		goto free_handle;
235 
236 	dri->context =
237 	    dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
238 
239 	if (!dri->context)
240 		goto free_screen;
241 
242 	if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
243 			      (const __DRIextension **)&dri->image_extension))
244 		goto free_context;
245 
246 	if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
247 			      (const __DRIextension **)&dri->flush_extension))
248 		goto free_context;
249 
250 	return 0;
251 
252 free_context:
253 	dri->core_extension->destroyContext(dri->context);
254 free_screen:
255 	dri->core_extension->destroyScreen(dri->device);
256 free_handle:
257 	dlclose(dri->driver_handle);
258 	dri->driver_handle = NULL;
259 close_dri_fd:
260 	close(dri->fd);
261 	return -ENODEV;
262 }
263 
264 /*
265  * The caller is responsible for freeing drv->priv.
266  */
dri_close(struct driver * drv)267 void dri_close(struct driver *drv)
268 {
269 	struct dri_driver *dri = drv->priv;
270 
271 	dri->core_extension->destroyContext(dri->context);
272 	dri->core_extension->destroyScreen(dri->device);
273 	dlclose(dri->driver_handle);
274 	dri->driver_handle = NULL;
275 	close(dri->fd);
276 }
277 
dri_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)278 int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
279 		  uint64_t use_flags)
280 {
281 	unsigned int dri_use;
282 	int ret, dri_format;
283 	struct dri_driver *dri = bo->drv->priv;
284 
285 	dri_format = drm_format_to_dri_format(format);
286 
287 	/* Gallium drivers require shared to get the handle and stride. */
288 	dri_use = __DRI_IMAGE_USE_SHARE;
289 	if (use_flags & BO_USE_SCANOUT)
290 		dri_use |= __DRI_IMAGE_USE_SCANOUT;
291 	if (use_flags & BO_USE_CURSOR)
292 		dri_use |= __DRI_IMAGE_USE_CURSOR;
293 	if (use_flags & BO_USE_LINEAR)
294 		dri_use |= __DRI_IMAGE_USE_LINEAR;
295 
296 	bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
297 						     dri_use, NULL);
298 	if (!bo->priv) {
299 		ret = -errno;
300 		return ret;
301 	}
302 
303 	ret = import_into_minigbm(dri, bo);
304 	if (ret)
305 		goto free_image;
306 
307 	return 0;
308 
309 free_image:
310 	dri->image_extension->destroyImage(bo->priv);
311 	return ret;
312 }
313 
dri_bo_create_with_modifiers(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,const uint64_t * modifiers,uint32_t modifier_count)314 int dri_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
315 				 const uint64_t *modifiers, uint32_t modifier_count)
316 {
317 	int ret, dri_format;
318 	struct dri_driver *dri = bo->drv->priv;
319 
320 	if (!dri->image_extension->createImageWithModifiers) {
321 		return -ENOENT;
322 	}
323 
324 	dri_format = drm_format_to_dri_format(format);
325 
326 	bo->priv = dri->image_extension->createImageWithModifiers(
327 	    dri->device, width, height, dri_format, modifiers, modifier_count, NULL);
328 	if (!bo->priv) {
329 		ret = -errno;
330 		return ret;
331 	}
332 
333 	ret = import_into_minigbm(dri, bo);
334 	if (ret)
335 		goto free_image;
336 
337 	return 0;
338 
339 free_image:
340 	dri->image_extension->destroyImage(bo->priv);
341 	return ret;
342 }
343 
dri_bo_import(struct bo * bo,struct drv_import_fd_data * data)344 int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
345 {
346 	int ret;
347 	struct dri_driver *dri = bo->drv->priv;
348 
349 	if (data->format_modifiers[0] != DRM_FORMAT_MOD_INVALID) {
350 		unsigned error;
351 
352 		if (!dri->image_extension->createImageFromDmaBufs2)
353 			return -ENOSYS;
354 
355 		// clang-format off
356 		bo->priv = dri->image_extension->createImageFromDmaBufs2(dri->device, data->width, data->height,
357 									 data->format,
358 									 data->format_modifiers[0],
359 									 data->fds,
360 									 bo->meta.num_planes,
361 									 (int *)data->strides,
362 									 (int *)data->offsets,
363 									 __DRI_YUV_COLOR_SPACE_UNDEFINED,
364 									 __DRI_YUV_RANGE_UNDEFINED,
365 									 __DRI_YUV_CHROMA_SITING_UNDEFINED,
366 									 __DRI_YUV_CHROMA_SITING_UNDEFINED,
367 									 &error, NULL);
368 		// clang-format on
369 
370 		/* Could translate the DRI error, but the Mesa GBM also returns ENOSYS. */
371 		if (!bo->priv)
372 			return -ENOSYS;
373 	} else {
374 		// clang-format off
375 		bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
376 								    data->format, data->fds,
377 								    bo->meta.num_planes,
378 								    (int *)data->strides,
379 								    (int *)data->offsets, NULL);
380 		// clang-format on
381 		if (!bo->priv)
382 			return -errno;
383 	}
384 
385 	ret = import_into_minigbm(dri, bo);
386 	if (ret) {
387 		dri->image_extension->destroyImage(bo->priv);
388 		return ret;
389 	}
390 
391 	return 0;
392 }
393 
dri_bo_destroy(struct bo * bo)394 int dri_bo_destroy(struct bo *bo)
395 {
396 	struct dri_driver *dri = bo->drv->priv;
397 
398 	assert(bo->priv);
399 	close_gem_handle(bo->handles[0].u32, bo->drv->fd);
400 	dri->image_extension->destroyImage(bo->priv);
401 	bo->priv = NULL;
402 	return 0;
403 }
404 
405 /*
406  * Map an image plane.
407  *
408  * This relies on the underlying driver to do a decompressing and/or de-tiling
409  * blit if necessary,
410  *
411  * This function itself is not thread-safe; we rely on the fact that the caller
412  * locks a per-driver mutex.
413  */
dri_bo_map(struct bo * bo,struct vma * vma,size_t plane,uint32_t map_flags)414 void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
415 {
416 	struct dri_driver *dri = bo->drv->priv;
417 
418 	/* GBM flags and DRI flags are the same. */
419 	vma->addr = dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->meta.width,
420 						   bo->meta.height, map_flags,
421 						   (int *)&vma->map_strides[plane], &vma->priv);
422 	if (!vma->addr)
423 		return MAP_FAILED;
424 
425 	return vma->addr;
426 }
427 
dri_bo_unmap(struct bo * bo,struct vma * vma)428 int dri_bo_unmap(struct bo *bo, struct vma *vma)
429 {
430 	struct dri_driver *dri = bo->drv->priv;
431 
432 	assert(vma->priv);
433 	dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
434 
435 	/*
436 	 * From gbm_dri.c in Mesa:
437 	 *
438 	 * "Not all DRI drivers use direct maps. They may queue up DMA operations
439 	 *  on the mapping context. Since there is no explicit gbm flush mechanism,
440 	 *  we need to flush here."
441 	 */
442 
443 	dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
444 	return 0;
445 }
446 
dri_num_planes_from_modifier(struct driver * drv,uint32_t format,uint64_t modifier)447 size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
448 {
449 	struct dri_driver *dri = drv->priv;
450 	if (!dri->image_extension->queryDmaBufFormatModifierAttribs) {
451 		/* We do not do any modifier checks here. The create will fail
452 		 * later if the modifier is not supported. */
453 		return drv_num_planes_from_format(format);
454 	}
455 
456 	uint64_t planes;
457 	GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
458 	    dri->device, format, modifier, __DRI_IMAGE_ATTRIB_NUM_PLANES, &planes);
459 	if (!ret)
460 		return 0;
461 
462 	return planes;
463 }
464 
465 #endif
466