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