blob: 3ee405bb7d076038267d87a97e4435c895cec5f0 [file] [log] [blame]
Satyajitcdcebd82018-01-12 14:49:05 +05301/*
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>
Satyajit Sahufaeb0092018-08-07 15:17:18 +053012#include <fcntl.h>
Satyajitcdcebd82018-01-12 14:49:05 +053013#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
25static 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 },
Bas Nieuwenhuizen457fb692020-07-14 14:15:38 +020037 { DRM_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XBGR2101010 },
Satyajitcdcebd82018-01-12 14:49:05 +053038 { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
Bas Nieuwenhuizen457fb692020-07-14 14:15:38 +020039 { DRM_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ABGR2101010 },
Satyajitcdcebd82018-01-12 14:49:05 +053040};
41
42static int drm_format_to_dri_format(uint32_t drm_format)
43{
44 uint32_t i;
45 for (i = 0; i < ARRAY_SIZE(drm_to_dri_image_formats); i++) {
46 if (drm_to_dri_image_formats[i].drm_format == drm_format)
47 return drm_to_dri_image_formats[i].dri_image_format;
48 }
49
50 return 0;
51}
52
53static bool lookup_extension(const __DRIextension *const *extensions, const char *name,
54 int min_version, const __DRIextension **dst)
55{
56 while (*extensions) {
57 if ((*extensions)->name && !strcmp((*extensions)->name, name) &&
58 (*extensions)->version >= min_version) {
59 *dst = *extensions;
60 return true;
61 }
62
63 extensions++;
64 }
65
66 return false;
67}
68
69/*
Satyajit Sahua8a38952018-06-27 12:11:12 +053070 * Close Gem Handle
71 */
72static void close_gem_handle(uint32_t handle, int fd)
73{
Gurchetan Singh99644382020-10-07 15:28:11 -070074 struct drm_gem_close gem_close = { 0 };
Satyajit Sahua8a38952018-06-27 12:11:12 +053075 int ret = 0;
76
Satyajit Sahua8a38952018-06-27 12:11:12 +053077 gem_close.handle = handle;
78 ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
79 if (ret)
80 drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", handle, ret);
81}
82
83/*
ChromeOS Developer9b367b32020-03-02 13:08:53 +010084 * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
85 * to import into minigbm.
86 */
87static int import_into_minigbm(struct dri_driver *dri, struct bo *bo)
88{
89 uint32_t handle;
90 int ret, modifier_upper, modifier_lower, num_planes, i, j;
Bas Nieuwenhuizen912c4c32020-03-13 11:21:34 +010091 off_t dmabuf_sizes[DRV_MAX_PLANES];
ChromeOS Developer9b367b32020-03-02 13:08:53 +010092 __DRIimage *plane_image = NULL;
93
94 if (dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
95 &modifier_upper) &&
96 dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
97 &modifier_lower)) {
Gurchetan Singh52155b42021-01-27 17:55:17 -080098 bo->meta.format_modifier =
ChromeOS Developer9b367b32020-03-02 13:08:53 +010099 ((uint64_t)modifier_upper << 32) | (uint32_t)modifier_lower;
100 } else {
Gurchetan Singh52155b42021-01-27 17:55:17 -0800101 bo->meta.format_modifier = DRM_FORMAT_MOD_INVALID;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100102 }
103
104 if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_NUM_PLANES,
105 &num_planes)) {
106 return -errno;
107 }
108
109 bo->meta.num_planes = num_planes;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100110 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
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100115 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride) ||
116 !dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
117 ret = -errno;
118 goto cleanup;
119 }
120
121 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &prime_fd)) {
122 ret = -errno;
123 goto cleanup;
124 }
125
Bas Nieuwenhuizen912c4c32020-03-13 11:21:34 +0100126 dmabuf_sizes[i] = lseek(prime_fd, 0, SEEK_END);
127 if (dmabuf_sizes[i] == (off_t)-1) {
128 ret = -errno;
129 close(prime_fd);
130 goto cleanup;
131 }
132
133 lseek(prime_fd, 0, SEEK_SET);
134
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100135 ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
136
137 close(prime_fd);
138
139 if (ret) {
140 drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
141 goto cleanup;
142 }
143
144 bo->handles[i].u32 = handle;
145
146 bo->meta.strides[i] = stride;
147 bo->meta.offsets[i] = offset;
148
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100149 if (plane_image)
150 dri->image_extension->destroyImage(plane_image);
151 }
152
Bas Nieuwenhuizen912c4c32020-03-13 11:21:34 +0100153 for (i = 0; i < num_planes; ++i) {
154 off_t next_plane = dmabuf_sizes[i];
155 for (j = 0; j < num_planes; ++j) {
156 if (bo->meta.offsets[j] < next_plane &&
157 bo->meta.offsets[j] > bo->meta.offsets[i] &&
158 bo->handles[j].u32 == bo->handles[i].u32)
159 next_plane = bo->meta.offsets[j];
160 }
161
162 bo->meta.sizes[i] = next_plane - bo->meta.offsets[i];
163
164 /* This is kind of misleading if different planes use
165 different dmabufs. */
166 bo->meta.total_size += bo->meta.sizes[i];
167 }
168
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100169 return 0;
170
171cleanup:
172 if (plane_image)
173 dri->image_extension->destroyImage(plane_image);
174 while (--i >= 0) {
175 for (j = 0; j <= i; ++j)
176 if (bo->handles[j].u32 == bo->handles[i].u32)
177 break;
178
179 /* Multiple equivalent handles) */
180 if (i == j)
181 break;
182
183 /* This kind of goes horribly wrong when we already imported
184 * the same handles earlier, as we should really reference
185 * count handles. */
186 close_gem_handle(bo->handles[i].u32, bo->drv->fd);
187 }
188 return ret;
189}
190
191/*
Satyajitcdcebd82018-01-12 14:49:05 +0530192 * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
193 */
194int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
195{
196 char fname[128];
197 const __DRIextension **(*get_extensions)();
198 const __DRIextension *loader_extensions[] = { NULL };
199
200 struct dri_driver *dri = drv->priv;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530201
202 dri->fd = open(drmGetRenderDeviceNameFromFd(drv_get_fd(drv)), O_RDWR);
203 if (dri->fd < 0)
204 return -ENODEV;
205
Satyajitcdcebd82018-01-12 14:49:05 +0530206 dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
207 if (!dri->driver_handle)
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530208 goto close_dri_fd;
Satyajitcdcebd82018-01-12 14:49:05 +0530209
210 snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
211 get_extensions = dlsym(dri->driver_handle, fname);
212 if (!get_extensions)
213 goto free_handle;
214
215 dri->extensions = get_extensions();
216 if (!dri->extensions)
217 goto free_handle;
218
219 if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
220 (const __DRIextension **)&dri->core_extension))
221 goto free_handle;
222
223 /* Version 4 for createNewScreen2 */
224 if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
225 (const __DRIextension **)&dri->dri2_extension))
226 goto free_handle;
227
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530228 dri->device = dri->dri2_extension->createNewScreen2(0, dri->fd, loader_extensions,
Satyajitcdcebd82018-01-12 14:49:05 +0530229 dri->extensions, &dri->configs, NULL);
230 if (!dri->device)
231 goto free_handle;
232
233 dri->context =
234 dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
235
236 if (!dri->context)
237 goto free_screen;
238
239 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
240 (const __DRIextension **)&dri->image_extension))
241 goto free_context;
242
243 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
244 (const __DRIextension **)&dri->flush_extension))
245 goto free_context;
246
247 return 0;
248
249free_context:
250 dri->core_extension->destroyContext(dri->context);
251free_screen:
252 dri->core_extension->destroyScreen(dri->device);
253free_handle:
254 dlclose(dri->driver_handle);
255 dri->driver_handle = NULL;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530256close_dri_fd:
257 close(dri->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530258 return -ENODEV;
259}
260
261/*
262 * The caller is responsible for freeing drv->priv.
263 */
264void dri_close(struct driver *drv)
265{
266 struct dri_driver *dri = drv->priv;
267
268 dri->core_extension->destroyContext(dri->context);
269 dri->core_extension->destroyScreen(dri->device);
270 dlclose(dri->driver_handle);
271 dri->driver_handle = NULL;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530272 close(dri->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530273}
274
275int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
276 uint64_t use_flags)
277{
278 unsigned int dri_use;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100279 int ret, dri_format;
Satyajitcdcebd82018-01-12 14:49:05 +0530280 struct dri_driver *dri = bo->drv->priv;
281
Satyajitcdcebd82018-01-12 14:49:05 +0530282 dri_format = drm_format_to_dri_format(format);
283
284 /* Gallium drivers require shared to get the handle and stride. */
285 dri_use = __DRI_IMAGE_USE_SHARE;
286 if (use_flags & BO_USE_SCANOUT)
287 dri_use |= __DRI_IMAGE_USE_SCANOUT;
288 if (use_flags & BO_USE_CURSOR)
289 dri_use |= __DRI_IMAGE_USE_CURSOR;
Satyajit Sahua0e602b2018-05-03 16:10:11 +0530290 if (use_flags & BO_USE_LINEAR)
Satyajitcdcebd82018-01-12 14:49:05 +0530291 dri_use |= __DRI_IMAGE_USE_LINEAR;
292
293 bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
294 dri_use, NULL);
295 if (!bo->priv) {
296 ret = -errno;
297 return ret;
298 }
299
300 ret = import_into_minigbm(dri, bo);
301 if (ret)
302 goto free_image;
303
Satyajitcdcebd82018-01-12 14:49:05 +0530304 return 0;
305
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100306free_image:
307 dri->image_extension->destroyImage(bo->priv);
308 return ret;
309}
310
311int dri_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
312 const uint64_t *modifiers, uint32_t modifier_count)
313{
314 int ret, dri_format;
315 struct dri_driver *dri = bo->drv->priv;
316
317 if (!dri->image_extension->createImageWithModifiers) {
318 return -ENOENT;
319 }
320
321 dri_format = drm_format_to_dri_format(format);
322
323 bo->priv = dri->image_extension->createImageWithModifiers(
324 dri->device, width, height, dri_format, modifiers, modifier_count, NULL);
325 if (!bo->priv) {
326 ret = -errno;
327 return ret;
328 }
329
330 ret = import_into_minigbm(dri, bo);
331 if (ret)
332 goto free_image;
333
334 return 0;
335
Satyajitcdcebd82018-01-12 14:49:05 +0530336free_image:
337 dri->image_extension->destroyImage(bo->priv);
338 return ret;
339}
340
341int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
342{
343 int ret;
344 struct dri_driver *dri = bo->drv->priv;
345
Gurchetan Singh52155b42021-01-27 17:55:17 -0800346 if (data->format_modifier != DRM_FORMAT_MOD_INVALID) {
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100347 unsigned error;
Satyajitcdcebd82018-01-12 14:49:05 +0530348
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100349 if (!dri->image_extension->createImageFromDmaBufs2)
350 return -ENOSYS;
351
352 // clang-format off
353 bo->priv = dri->image_extension->createImageFromDmaBufs2(dri->device, data->width, data->height,
Roman Stratiienko142dd9c2020-12-14 17:34:09 +0200354 drv_get_standard_fourcc(data->format),
Gurchetan Singh52155b42021-01-27 17:55:17 -0800355 data->format_modifier,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100356 data->fds,
357 bo->meta.num_planes,
358 (int *)data->strides,
359 (int *)data->offsets,
360 __DRI_YUV_COLOR_SPACE_UNDEFINED,
361 __DRI_YUV_RANGE_UNDEFINED,
362 __DRI_YUV_CHROMA_SITING_UNDEFINED,
363 __DRI_YUV_CHROMA_SITING_UNDEFINED,
364 &error, NULL);
365 // clang-format on
366
367 /* Could translate the DRI error, but the Mesa GBM also returns ENOSYS. */
368 if (!bo->priv)
369 return -ENOSYS;
370 } else {
371 // clang-format off
372 bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
Roman Stratiienko142dd9c2020-12-14 17:34:09 +0200373 drv_get_standard_fourcc(data->format), data->fds,
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100374 bo->meta.num_planes,
375 (int *)data->strides,
376 (int *)data->offsets, NULL);
377 // clang-format on
378 if (!bo->priv)
379 return -errno;
380 }
Satyajitcdcebd82018-01-12 14:49:05 +0530381
382 ret = import_into_minigbm(dri, bo);
383 if (ret) {
384 dri->image_extension->destroyImage(bo->priv);
385 return ret;
386 }
387
388 return 0;
389}
390
391int dri_bo_destroy(struct bo *bo)
392{
393 struct dri_driver *dri = bo->drv->priv;
394
395 assert(bo->priv);
Satyajit Sahua8a38952018-06-27 12:11:12 +0530396 close_gem_handle(bo->handles[0].u32, bo->drv->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530397 dri->image_extension->destroyImage(bo->priv);
398 bo->priv = NULL;
399 return 0;
400}
401
402/*
403 * Map an image plane.
404 *
405 * This relies on the underlying driver to do a decompressing and/or de-tiling
406 * blit if necessary,
407 *
408 * This function itself is not thread-safe; we rely on the fact that the caller
409 * locks a per-driver mutex.
410 */
411void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
412{
413 struct dri_driver *dri = bo->drv->priv;
Satyajitcdcebd82018-01-12 14:49:05 +0530414
415 /* GBM flags and DRI flags are the same. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700416 vma->addr = dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->meta.width,
417 bo->meta.height, map_flags,
418 (int *)&vma->map_strides[plane], &vma->priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530419 if (!vma->addr)
420 return MAP_FAILED;
421
422 return vma->addr;
423}
424
425int dri_bo_unmap(struct bo *bo, struct vma *vma)
426{
427 struct dri_driver *dri = bo->drv->priv;
428
429 assert(vma->priv);
430 dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
431
432 /*
433 * From gbm_dri.c in Mesa:
434 *
435 * "Not all DRI drivers use direct maps. They may queue up DMA operations
436 * on the mapping context. Since there is no explicit gbm flush mechanism,
437 * we need to flush here."
438 */
439
440 dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
441 return 0;
442}
443
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100444size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
445{
446 struct dri_driver *dri = drv->priv;
447 if (!dri->image_extension->queryDmaBufFormatModifierAttribs) {
448 /* We do not do any modifier checks here. The create will fail
449 * later if the modifier is not supported. */
450 return drv_num_planes_from_format(format);
451 }
452
453 uint64_t planes;
454 GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
455 dri->device, format, modifier, __DRI_IMAGE_ATTRIB_NUM_PLANES, &planes);
456 if (!ret)
457 return 0;
458
459 return planes;
460}
461
Satyajitcdcebd82018-01-12 14:49:05 +0530462#endif