blob: 6d7c9a0e04d57cb4ed22d0291a3fa2e240d7e074 [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 },
37 { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
38};
39
40static 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
51static 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/*
Satyajit Sahua8a38952018-06-27 12:11:12 +053068 * Close Gem Handle
69 */
70static 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/*
ChromeOS Developer9b367b32020-03-02 13:08:53 +010083 * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
84 * to import into minigbm.
85 */
86static 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 __DRIimage *plane_image = NULL;
91
92 if (dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
93 &modifier_upper) &&
94 dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
95 &modifier_lower)) {
96 bo->meta.format_modifiers[0] =
97 ((uint64_t)modifier_upper << 32) | (uint32_t)modifier_lower;
98 } else {
99 bo->meta.format_modifiers[0] = DRM_FORMAT_MOD_INVALID;
100 }
101
102 if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_NUM_PLANES,
103 &num_planes)) {
104 return -errno;
105 }
106
107 bo->meta.num_planes = num_planes;
108
109 for (i = 0; i < num_planes; ++i) {
110 int prime_fd, stride, offset;
111 plane_image = dri->image_extension->fromPlanar(bo->priv, i, NULL);
112 __DRIimage *image = plane_image ? plane_image : bo->priv;
113
114 if (i)
115 bo->meta.format_modifiers[i] = bo->meta.format_modifiers[0];
116
117 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride) ||
118 !dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
119 ret = -errno;
120 goto cleanup;
121 }
122
123 if (!dri->image_extension->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &prime_fd)) {
124 ret = -errno;
125 goto cleanup;
126 }
127
128 ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
129
130 close(prime_fd);
131
132 if (ret) {
133 drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
134 goto cleanup;
135 }
136
137 bo->handles[i].u32 = handle;
138
139 bo->meta.strides[i] = stride;
140 bo->meta.offsets[i] = offset;
141
142 /* Not setting sizes[i] and total_size as these are not
143 * provided by DRI. The best way to "approximate" would be
144 * what drv_bo_import does, but I see nothing in the minigbm
145 * core that needs it. */
146
147 if (plane_image)
148 dri->image_extension->destroyImage(plane_image);
149 }
150
151 return 0;
152
153cleanup:
154 if (plane_image)
155 dri->image_extension->destroyImage(plane_image);
156 while (--i >= 0) {
157 for (j = 0; j <= i; ++j)
158 if (bo->handles[j].u32 == bo->handles[i].u32)
159 break;
160
161 /* Multiple equivalent handles) */
162 if (i == j)
163 break;
164
165 /* This kind of goes horribly wrong when we already imported
166 * the same handles earlier, as we should really reference
167 * count handles. */
168 close_gem_handle(bo->handles[i].u32, bo->drv->fd);
169 }
170 return ret;
171}
172
173/*
Satyajitcdcebd82018-01-12 14:49:05 +0530174 * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
175 */
176int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
177{
178 char fname[128];
179 const __DRIextension **(*get_extensions)();
180 const __DRIextension *loader_extensions[] = { NULL };
181
182 struct dri_driver *dri = drv->priv;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530183
184 dri->fd = open(drmGetRenderDeviceNameFromFd(drv_get_fd(drv)), O_RDWR);
185 if (dri->fd < 0)
186 return -ENODEV;
187
Satyajitcdcebd82018-01-12 14:49:05 +0530188 dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
189 if (!dri->driver_handle)
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530190 goto close_dri_fd;
Satyajitcdcebd82018-01-12 14:49:05 +0530191
192 snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
193 get_extensions = dlsym(dri->driver_handle, fname);
194 if (!get_extensions)
195 goto free_handle;
196
197 dri->extensions = get_extensions();
198 if (!dri->extensions)
199 goto free_handle;
200
201 if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
202 (const __DRIextension **)&dri->core_extension))
203 goto free_handle;
204
205 /* Version 4 for createNewScreen2 */
206 if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
207 (const __DRIextension **)&dri->dri2_extension))
208 goto free_handle;
209
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530210 dri->device = dri->dri2_extension->createNewScreen2(0, dri->fd, loader_extensions,
Satyajitcdcebd82018-01-12 14:49:05 +0530211 dri->extensions, &dri->configs, NULL);
212 if (!dri->device)
213 goto free_handle;
214
215 dri->context =
216 dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
217
218 if (!dri->context)
219 goto free_screen;
220
221 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
222 (const __DRIextension **)&dri->image_extension))
223 goto free_context;
224
225 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
226 (const __DRIextension **)&dri->flush_extension))
227 goto free_context;
228
229 return 0;
230
231free_context:
232 dri->core_extension->destroyContext(dri->context);
233free_screen:
234 dri->core_extension->destroyScreen(dri->device);
235free_handle:
236 dlclose(dri->driver_handle);
237 dri->driver_handle = NULL;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530238close_dri_fd:
239 close(dri->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530240 return -ENODEV;
241}
242
243/*
244 * The caller is responsible for freeing drv->priv.
245 */
246void dri_close(struct driver *drv)
247{
248 struct dri_driver *dri = drv->priv;
249
250 dri->core_extension->destroyContext(dri->context);
251 dri->core_extension->destroyScreen(dri->device);
252 dlclose(dri->driver_handle);
253 dri->driver_handle = NULL;
Satyajit Sahufaeb0092018-08-07 15:17:18 +0530254 close(dri->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530255}
256
257int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
258 uint64_t use_flags)
259{
260 unsigned int dri_use;
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100261 int ret, dri_format;
Satyajitcdcebd82018-01-12 14:49:05 +0530262 struct dri_driver *dri = bo->drv->priv;
263
Satyajitcdcebd82018-01-12 14:49:05 +0530264 dri_format = drm_format_to_dri_format(format);
265
266 /* Gallium drivers require shared to get the handle and stride. */
267 dri_use = __DRI_IMAGE_USE_SHARE;
268 if (use_flags & BO_USE_SCANOUT)
269 dri_use |= __DRI_IMAGE_USE_SCANOUT;
270 if (use_flags & BO_USE_CURSOR)
271 dri_use |= __DRI_IMAGE_USE_CURSOR;
Satyajit Sahua0e602b2018-05-03 16:10:11 +0530272 if (use_flags & BO_USE_LINEAR)
Satyajitcdcebd82018-01-12 14:49:05 +0530273 dri_use |= __DRI_IMAGE_USE_LINEAR;
274
275 bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
276 dri_use, NULL);
277 if (!bo->priv) {
278 ret = -errno;
279 return ret;
280 }
281
282 ret = import_into_minigbm(dri, bo);
283 if (ret)
284 goto free_image;
285
Satyajitcdcebd82018-01-12 14:49:05 +0530286 return 0;
287
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100288free_image:
289 dri->image_extension->destroyImage(bo->priv);
290 return ret;
291}
292
293int dri_bo_create_with_modifiers(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
294 const uint64_t *modifiers, uint32_t modifier_count)
295{
296 int ret, dri_format;
297 struct dri_driver *dri = bo->drv->priv;
298
299 if (!dri->image_extension->createImageWithModifiers) {
300 return -ENOENT;
301 }
302
303 dri_format = drm_format_to_dri_format(format);
304
305 bo->priv = dri->image_extension->createImageWithModifiers(
306 dri->device, width, height, dri_format, modifiers, modifier_count, NULL);
307 if (!bo->priv) {
308 ret = -errno;
309 return ret;
310 }
311
312 ret = import_into_minigbm(dri, bo);
313 if (ret)
314 goto free_image;
315
316 return 0;
317
Satyajitcdcebd82018-01-12 14:49:05 +0530318free_image:
319 dri->image_extension->destroyImage(bo->priv);
320 return ret;
321}
322
323int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
324{
325 int ret;
326 struct dri_driver *dri = bo->drv->priv;
327
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100328 if (data->format_modifiers[0] != DRM_FORMAT_MOD_INVALID) {
329 unsigned error;
Satyajitcdcebd82018-01-12 14:49:05 +0530330
ChromeOS Developer9b367b32020-03-02 13:08:53 +0100331 if (!dri->image_extension->createImageFromDmaBufs2)
332 return -ENOSYS;
333
334 // clang-format off
335 bo->priv = dri->image_extension->createImageFromDmaBufs2(dri->device, data->width, data->height,
336 data->format,
337 data->format_modifiers[0],
338 data->fds,
339 bo->meta.num_planes,
340 (int *)data->strides,
341 (int *)data->offsets,
342 __DRI_YUV_COLOR_SPACE_UNDEFINED,
343 __DRI_YUV_RANGE_UNDEFINED,
344 __DRI_YUV_CHROMA_SITING_UNDEFINED,
345 __DRI_YUV_CHROMA_SITING_UNDEFINED,
346 &error, NULL);
347 // clang-format on
348
349 /* Could translate the DRI error, but the Mesa GBM also returns ENOSYS. */
350 if (!bo->priv)
351 return -ENOSYS;
352 } else {
353 // clang-format off
354 bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
355 data->format, data->fds,
356 bo->meta.num_planes,
357 (int *)data->strides,
358 (int *)data->offsets, NULL);
359 // clang-format on
360 if (!bo->priv)
361 return -errno;
362 }
Satyajitcdcebd82018-01-12 14:49:05 +0530363
364 ret = import_into_minigbm(dri, bo);
365 if (ret) {
366 dri->image_extension->destroyImage(bo->priv);
367 return ret;
368 }
369
370 return 0;
371}
372
373int dri_bo_destroy(struct bo *bo)
374{
375 struct dri_driver *dri = bo->drv->priv;
376
377 assert(bo->priv);
Satyajit Sahua8a38952018-06-27 12:11:12 +0530378 close_gem_handle(bo->handles[0].u32, bo->drv->fd);
Satyajitcdcebd82018-01-12 14:49:05 +0530379 dri->image_extension->destroyImage(bo->priv);
380 bo->priv = NULL;
381 return 0;
382}
383
384/*
385 * Map an image plane.
386 *
387 * This relies on the underlying driver to do a decompressing and/or de-tiling
388 * blit if necessary,
389 *
390 * This function itself is not thread-safe; we rely on the fact that the caller
391 * locks a per-driver mutex.
392 */
393void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
394{
395 struct dri_driver *dri = bo->drv->priv;
Satyajitcdcebd82018-01-12 14:49:05 +0530396
397 /* GBM flags and DRI flags are the same. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700398 vma->addr = dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->meta.width,
399 bo->meta.height, map_flags,
400 (int *)&vma->map_strides[plane], &vma->priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530401 if (!vma->addr)
402 return MAP_FAILED;
403
404 return vma->addr;
405}
406
407int dri_bo_unmap(struct bo *bo, struct vma *vma)
408{
409 struct dri_driver *dri = bo->drv->priv;
410
411 assert(vma->priv);
412 dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
413
414 /*
415 * From gbm_dri.c in Mesa:
416 *
417 * "Not all DRI drivers use direct maps. They may queue up DMA operations
418 * on the mapping context. Since there is no explicit gbm flush mechanism,
419 * we need to flush here."
420 */
421
422 dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
423 return 0;
424}
425
ChromeOS Developer44588bb2020-03-02 16:32:09 +0100426size_t dri_num_planes_from_modifier(struct driver *drv, uint32_t format, uint64_t modifier)
427{
428 struct dri_driver *dri = drv->priv;
429 if (!dri->image_extension->queryDmaBufFormatModifierAttribs) {
430 /* We do not do any modifier checks here. The create will fail
431 * later if the modifier is not supported. */
432 return drv_num_planes_from_format(format);
433 }
434
435 uint64_t planes;
436 GLboolean ret = dri->image_extension->queryDmaBufFormatModifierAttribs(
437 dri->device, format, modifier, __DRI_IMAGE_ATTRIB_NUM_PLANES, &planes);
438 if (!ret)
439 return 0;
440
441 return planes;
442}
443
Satyajitcdcebd82018-01-12 14:49:05 +0530444#endif