blob: ae491bb01d591e69e5f15eea3742d93da2688cfd [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>
12#include <stdbool.h>
13#include <stdio.h>
14#include <string.h>
15#include <sys/mman.h>
16#include <unistd.h>
17#include <xf86drm.h>
18
19#include "dri.h"
20#include "drv_priv.h"
21#include "helpers.h"
22#include "util.h"
23
24static const struct {
25 uint32_t drm_format;
26 int dri_image_format;
27} drm_to_dri_image_formats[] = {
28 { DRM_FORMAT_R8, __DRI_IMAGE_FORMAT_R8 },
29 { DRM_FORMAT_GR88, __DRI_IMAGE_FORMAT_GR88 },
30 { DRM_FORMAT_RGB565, __DRI_IMAGE_FORMAT_RGB565 },
31 { DRM_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_XRGB8888 },
32 { DRM_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_ARGB8888 },
33 { DRM_FORMAT_XBGR8888, __DRI_IMAGE_FORMAT_XBGR8888 },
34 { DRM_FORMAT_ABGR8888, __DRI_IMAGE_FORMAT_ABGR8888 },
35 { DRM_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XRGB2101010 },
36 { DRM_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ARGB2101010 },
37};
38
39static int drm_format_to_dri_format(uint32_t drm_format)
40{
41 uint32_t i;
42 for (i = 0; i < ARRAY_SIZE(drm_to_dri_image_formats); i++) {
43 if (drm_to_dri_image_formats[i].drm_format == drm_format)
44 return drm_to_dri_image_formats[i].dri_image_format;
45 }
46
47 return 0;
48}
49
50static bool lookup_extension(const __DRIextension *const *extensions, const char *name,
51 int min_version, const __DRIextension **dst)
52{
53 while (*extensions) {
54 if ((*extensions)->name && !strcmp((*extensions)->name, name) &&
55 (*extensions)->version >= min_version) {
56 *dst = *extensions;
57 return true;
58 }
59
60 extensions++;
61 }
62
63 return false;
64}
65
66/*
67 * The DRI GEM namespace may be different from the minigbm's driver GEM namespace. We have
68 * to import into minigbm.
69 */
70static int import_into_minigbm(struct dri_driver *dri, struct bo *bo)
71{
72 uint32_t handle;
73 int prime_fd, ret;
74
75 if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_FD, &prime_fd))
76 return -errno;
77
78 ret = drmPrimeFDToHandle(bo->drv->fd, prime_fd, &handle);
79 if (ret) {
80 drv_log("drmPrimeFDToHandle failed with %s\n", strerror(errno));
81 return ret;
82 }
83
84 bo->handles[0].u32 = handle;
85 close(prime_fd);
86 return 0;
87}
88
89/*
90 * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
91 */
92int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
93{
94 char fname[128];
95 const __DRIextension **(*get_extensions)();
96 const __DRIextension *loader_extensions[] = { NULL };
97
98 struct dri_driver *dri = drv->priv;
99 dri->driver_handle = dlopen(dri_so_path, RTLD_NOW | RTLD_GLOBAL);
100 if (!dri->driver_handle)
101 return -ENODEV;
102
103 snprintf(fname, sizeof(fname), __DRI_DRIVER_GET_EXTENSIONS "_%s", driver_suffix);
104 get_extensions = dlsym(dri->driver_handle, fname);
105 if (!get_extensions)
106 goto free_handle;
107
108 dri->extensions = get_extensions();
109 if (!dri->extensions)
110 goto free_handle;
111
112 if (!lookup_extension(dri->extensions, __DRI_CORE, 2,
113 (const __DRIextension **)&dri->core_extension))
114 goto free_handle;
115
116 /* Version 4 for createNewScreen2 */
117 if (!lookup_extension(dri->extensions, __DRI_DRI2, 4,
118 (const __DRIextension **)&dri->dri2_extension))
119 goto free_handle;
120
121 dri->device = dri->dri2_extension->createNewScreen2(0, drv_get_fd(drv), loader_extensions,
122 dri->extensions, &dri->configs, NULL);
123 if (!dri->device)
124 goto free_handle;
125
126 dri->context =
127 dri->dri2_extension->createNewContext(dri->device, *dri->configs, NULL, NULL);
128
129 if (!dri->context)
130 goto free_screen;
131
132 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI_IMAGE, 12,
133 (const __DRIextension **)&dri->image_extension))
134 goto free_context;
135
136 if (!lookup_extension(dri->core_extension->getExtensions(dri->device), __DRI2_FLUSH, 4,
137 (const __DRIextension **)&dri->flush_extension))
138 goto free_context;
139
140 return 0;
141
142free_context:
143 dri->core_extension->destroyContext(dri->context);
144free_screen:
145 dri->core_extension->destroyScreen(dri->device);
146free_handle:
147 dlclose(dri->driver_handle);
148 dri->driver_handle = NULL;
149 return -ENODEV;
150}
151
152/*
153 * The caller is responsible for freeing drv->priv.
154 */
155void dri_close(struct driver *drv)
156{
157 struct dri_driver *dri = drv->priv;
158
159 dri->core_extension->destroyContext(dri->context);
160 dri->core_extension->destroyScreen(dri->device);
161 dlclose(dri->driver_handle);
162 dri->driver_handle = NULL;
163}
164
165int dri_bo_create(struct bo *bo, uint32_t width, uint32_t height, uint32_t format,
166 uint64_t use_flags)
167{
168 unsigned int dri_use;
169 int ret, dri_format, stride, offset;
170 struct dri_driver *dri = bo->drv->priv;
171
172 assert(bo->num_planes == 1);
173 dri_format = drm_format_to_dri_format(format);
174
175 /* Gallium drivers require shared to get the handle and stride. */
176 dri_use = __DRI_IMAGE_USE_SHARE;
177 if (use_flags & BO_USE_SCANOUT)
178 dri_use |= __DRI_IMAGE_USE_SCANOUT;
179 if (use_flags & BO_USE_CURSOR)
180 dri_use |= __DRI_IMAGE_USE_CURSOR;
Satyajit Sahua0e602b2018-05-03 16:10:11 +0530181 if (use_flags & BO_USE_LINEAR)
Satyajitcdcebd82018-01-12 14:49:05 +0530182 dri_use |= __DRI_IMAGE_USE_LINEAR;
183
184 bo->priv = dri->image_extension->createImage(dri->device, width, height, dri_format,
185 dri_use, NULL);
186 if (!bo->priv) {
187 ret = -errno;
188 return ret;
189 }
190
191 ret = import_into_minigbm(dri, bo);
192 if (ret)
193 goto free_image;
194
195 if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_STRIDE, &stride)) {
196 ret = -errno;
197 goto free_image;
198 }
199
200 if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
201 ret = -errno;
202 goto free_image;
203 }
204
205 bo->strides[0] = stride;
206 bo->sizes[0] = stride * height;
207 bo->offsets[0] = offset;
208 bo->total_size = offset + bo->sizes[0];
209 return 0;
210
211free_image:
212 dri->image_extension->destroyImage(bo->priv);
213 return ret;
214}
215
216int dri_bo_import(struct bo *bo, struct drv_import_fd_data *data)
217{
218 int ret;
219 struct dri_driver *dri = bo->drv->priv;
220
221 assert(bo->num_planes == 1);
222
223 // clang-format off
224 bo->priv = dri->image_extension->createImageFromFds(dri->device, data->width, data->height,
225 data->format, data->fds, bo->num_planes,
226 (int *)data->strides,
227 (int *)data->offsets, NULL);
228 // clang-format on
229 if (!bo->priv)
230 return -errno;
231
232 ret = import_into_minigbm(dri, bo);
233 if (ret) {
234 dri->image_extension->destroyImage(bo->priv);
235 return ret;
236 }
237
238 return 0;
239}
240
241int dri_bo_destroy(struct bo *bo)
242{
243 struct dri_driver *dri = bo->drv->priv;
244
245 assert(bo->priv);
246 dri->image_extension->destroyImage(bo->priv);
247 bo->priv = NULL;
248 return 0;
249}
250
251/*
252 * Map an image plane.
253 *
254 * This relies on the underlying driver to do a decompressing and/or de-tiling
255 * blit if necessary,
256 *
257 * This function itself is not thread-safe; we rely on the fact that the caller
258 * locks a per-driver mutex.
259 */
260void *dri_bo_map(struct bo *bo, struct vma *vma, size_t plane, uint32_t map_flags)
261{
262 struct dri_driver *dri = bo->drv->priv;
Satyajitcdcebd82018-01-12 14:49:05 +0530263
264 /* GBM flags and DRI flags are the same. */
Satyajit Sahu77b70552018-05-03 16:35:24 +0530265 vma->addr =
266 dri->image_extension->mapImage(dri->context, bo->priv, 0, 0, bo->width, bo->height,
267 map_flags, (int *)&vma->map_strides[plane], &vma->priv);
Satyajitcdcebd82018-01-12 14:49:05 +0530268 if (!vma->addr)
269 return MAP_FAILED;
270
271 return vma->addr;
272}
273
274int dri_bo_unmap(struct bo *bo, struct vma *vma)
275{
276 struct dri_driver *dri = bo->drv->priv;
277
278 assert(vma->priv);
279 dri->image_extension->unmapImage(dri->context, bo->priv, vma->priv);
280
281 /*
282 * From gbm_dri.c in Mesa:
283 *
284 * "Not all DRI drivers use direct maps. They may queue up DMA operations
285 * on the mapping context. Since there is no explicit gbm flush mechanism,
286 * we need to flush here."
287 */
288
289 dri->flush_extension->flush_with_flags(dri->context, NULL, __DRI2_FLUSH_CONTEXT, 0);
290 return 0;
291}
292
293#endif