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_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
200 dri->fd = open(drmGetRenderDeviceNameFromFd(drv_get_fd(drv)), O_RDWR);
201 if (dri->fd < 0)
202 return -ENODEV;
203
204 dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
205 if (!dri->driver_handle)
206 goto close_dri_fd;
207
208 snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
209 get_extensions = dlsym(dri->driver_handle, fname);
210 if (!get_extensions)
211 goto free_handle;
212
213 dri->extensions = get_extensions();
214 if (!dri->extensions)
215 goto free_handle;
216
217 if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
218 (const __DRIextension **)&dri->core_extension))
219 goto free_handle;
220
221 /* Version 4 for createNewScreen2 */
222 if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
223 (const __DRIextension **)&dri->dri2_extension))
224 goto free_handle;
225
226 dri->device = dri->dri2_extension->createNewScreen2(0, dri->fd, loader_extensions,
227 dri->extensions, &dri->configs, NULL);
228 if (!dri->device)
229 goto free_handle;
230
231 dri->context =
232 dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
233
234 if (!dri->context)
235 goto free_screen;
236
237 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
238 (const __DRIextension **)&dri->image_extension))
239 goto free_context;
240
241 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
242 (const __DRIextension **)&dri->flush_extension))
243 goto free_context;
244
245 return 0;
246
247 free_context:
248 dri->core_extension->destroyContext(dri->context);
249 free_screen:
250 dri->core_extension->destroyScreen(dri->device);
251 free_handle:
252 dlclose(dri->driver_handle);
253 dri->driver_handle = NULL;
254 close_dri_fd:
255 close(dri->fd);
256 return -ENODEV;
257 }
258
259 /*
260 * The caller is responsible for freeing drv->priv.
261 */
dri_close(struct driver * drv)262 void dri_close(struct driver *drv)
263 {
264 struct dri_driver *dri = drv->priv;
265
266 dri->core_extension->destroyContext(dri->context);
267 dri->core_extension->destroyScreen(dri->device);
268 dlclose(dri->driver_handle);
269 dri->driver_handle = NULL;
270 close(dri->fd);
271 }
272
dri_bo_create(struct bo * bo,uint32_t width,uint32_t height,uint32_t format,uint64_t use_flags)273 int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
274 uint64_t use_flags)
275 {
276 unsigned int dri_use;
277 int ret, dri_format;
278 struct dri_driver *dri = bo->drv->priv;
279
280 dri_format = drm_format_to_dri_format(format);
281
282 /* Gallium drivers require shared to get the handle and stride. */
283 dri_use = __DRI_IMAGE_USE_SHARE;
284 if (use_flags & BO_USE_SCANOUT)
285 dri_use |= __DRI_IMAGE_USE_SCANOUT;
286 if (use_flags & BO_USE_CURSOR)
287 dri_use |= __DRI_IMAGE_USE_CURSOR;
288 if (use_flags & BO_USE_LINEAR)
289 dri_use |= __DRI_IMAGE_USE_LINEAR;
290
291 bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
292 dri_use, NULL);
293 if (!bo->priv) {
294 ret = -errno;
295 return ret;
296 }
297
298 ret = import_into_minigbm(dri, bo);
299 if (ret)
300 goto free_image;
301
302 return 0;
303
304 free_image:
305 dri->image_extension->destroyImage(bo->priv);
306 return ret;
307 }
308
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)309 int dri_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
310 const uint64_t *modifiers, uint32_t modifier_count)
311 {
312 int ret, dri_format;
313 struct dri_driver *dri = bo->drv->priv;
314
315 if (!dri->image_extension->createImageWithModifiers)
316 return -ENOENT;
317
318 dri_format = drm_format_to_dri_format(format);
319
320 bo->priv = dri->image_extension->createImageWithModifiers(
321 dri->device, width, height, dri_format, modifiers, modifier_count, NULL);
322 if (!bo->priv) {
323 ret = -errno;
324 return ret;
325 }
326
327 ret = import_into_minigbm(dri, bo);
328 if (ret)
329 goto free_image;
330
331 return 0;
332
333 free_image:
334 dri->image_extension->destroyImage(bo->priv);
335 return ret;
336 }
337
dri_bo_import(struct bo * bo,struct drv_import_fd_data * data)338 int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
339 {
340 int ret;
341 struct dri_driver *dri = bo->drv->priv;
342
343 if (data->format_modifier != DRM_FORMAT_MOD_INVALID) {
344 unsigned error;
345
346 if (!dri->image_extension->createImageFromDmaBufs2)
347 return -ENOSYS;
348
349 // clang-format off
350 bo->priv = dri->image_extension->createImageFromDmaBufs2(dri->device, data->width, data->height,
351 drv_get_standard_fourcc(data->format),
352 data->format_modifier,
353 data->fds,
354 bo->meta.num_planes,
355 (int *)data->strides,
356 (int *)data->offsets,
357 __DRI_YUV_COLOR_SPACE_UNDEFINED,
358 __DRI_YUV_RANGE_UNDEFINED,
359 __DRI_YUV_CHROMA_SITING_UNDEFINED,
360 __DRI_YUV_CHROMA_SITING_UNDEFINED,
361 &error, NULL);
362 // clang-format on
363
364 /* Could translate the DRI error, but the Mesa GBM also returns ENOSYS. */
365 if (!bo->priv)
366 return -ENOSYS;
367 } else {
368 // clang-format off
369 bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
370 drv_get_standard_fourcc(data->format), data->fds,
371 bo->meta.num_planes,
372 (int *)data->strides,
373 (int *)data->offsets, NULL);
374 // clang-format on
375 if (!bo->priv)
376 return -errno;
377 }
378
379 ret = import_into_minigbm(dri, bo);
380 if (ret) {
381 dri->image_extension->destroyImage(bo->priv);
382 return ret;
383 }
384
385 return 0;
386 }
387
dri_bo_destroy(struct bo * bo)388 int dri_bo_destroy(struct bo *bo)
389 {
390 struct dri_driver *dri = bo->drv->priv;
391
392 assert(bo->priv);
393 close_gem_handle(bo->handles[0].u32, bo->drv->fd);
394 dri->image_extension->destroyImage(bo->priv);
395 bo->priv = NULL;
396 return 0;
397 }
398
399 /*
400 * Map an image plane.
401 *
402 * This relies on the underlying driver to do a decompressing and/or de-tiling
403 * blit if necessary,
404 *
405 * This function itself is not thread-safe; we rely on the fact that the caller
406 * locks a per-driver mutex.
407 */
dri_bo_map(struct bo * bo,struct vma * vma,size_t plane,uint32_t map_flags)408 void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
409 {
410 struct dri_driver *dri = bo->drv->priv;
411
412 /* GBM flags and DRI flags are the same. */
413 vma->addr = dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->meta.width,
414 bo->meta.height, map_flags,
415 (int *)&vma->map_strides[plane], &vma->priv);
416 if (!vma->addr)
417 return MAP_FAILED;
418
419 return vma->addr;
420 }
421
dri_bo_unmap(struct bo * bo,struct vma * vma)422 int dri_bo_unmap(struct bo *bo, struct vma *vma)
423 {
424 struct dri_driver *dri = bo->drv->priv;
425
426 assert(vma->priv);
427 dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
428
429 /*
430 * From gbm_dri.c in Mesa:
431 *
432 * "Not all DRI drivers use direct maps. They may queue up DMA operations
433 * on the mapping context. Since there is no explicit gbm flush mechanism,
434 * we need to flush here."
435 */
436
437 dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
438 return 0;
439 }
440
dri_num_planes_from_modifier(struct driver * drv,uint32_t format,uint64_t modifier)441 size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
442 {
443 struct dri_driver *dri = drv->priv;
444 if (!dri->image_extension->queryDmaBufFormatModifierAttribs) {
445 /* We do not do any modifier checks here. The create will fail
446 * later if the modifier is not supported. */
447 return drv_num_planes_from_format(format);
448 }
449
450 uint64_t planes;
451 GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
452 dri->device, format, modifier, __DRI_IMAGE_ATTRIB_NUM_PLANES, &planes);
453 if (!ret)
454 return 0;
455
456 return planes;
457 }
458
459 #endif
460