OSDN Git Service

minigbm: Add mmap() in backends
[android-x86/external-minigbm.git] / drv.c
1 /*
2  * Copyright (c) 2016 The Chromium OS Authors. 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 #include <assert.h>
7 #include <fcntl.h>
8 #include <pthread.h>
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <xf86drm.h>
15
16 #include "drv_priv.h"
17 #include "helpers.h"
18 #include "util.h"
19
20 extern struct backend backend_cirrus;
21 extern struct backend backend_evdi;
22 #ifdef DRV_EXYNOS
23 extern struct backend backend_exynos;
24 #endif
25 extern struct backend backend_gma500;
26 #ifdef DRV_I915
27 extern struct backend backend_i915;
28 #endif
29 #ifdef DRV_MARVELL
30 extern struct backend backend_marvell;
31 #endif
32 #ifdef DRV_MEDIATEK
33 extern struct backend backend_mediatek;
34 #endif
35 #ifdef DRV_ROCKCHIP
36 extern struct backend backend_rockchip;
37 #endif
38 #ifdef DRV_TEGRA
39 extern struct backend backend_tegra;
40 #endif
41 extern struct backend backend_udl;
42 extern struct backend backend_virtio_gpu;
43
44 static struct backend *drv_get_backend(int fd)
45 {
46         drmVersionPtr drm_version;
47         unsigned int i;
48
49         drm_version = drmGetVersion(fd);
50
51         if (!drm_version)
52                 return NULL;
53
54         struct backend *backend_list[] = {
55                 &backend_cirrus,
56                 &backend_evdi,
57 #ifdef DRV_EXYNOS
58                 &backend_exynos,
59 #endif
60                 &backend_gma500,
61 #ifdef DRV_I915
62                 &backend_i915,
63 #endif
64 #ifdef DRV_MARVELL
65                 &backend_marvell,
66 #endif
67 #ifdef DRV_MEDIATEK
68                 &backend_mediatek,
69 #endif
70 #ifdef DRV_ROCKCHIP
71                 &backend_rockchip,
72 #endif
73 #ifdef DRV_TEGRA
74                 &backend_tegra,
75 #endif
76                 &backend_udl,
77                 &backend_virtio_gpu,
78         };
79
80         for(i = 0; i < ARRAY_SIZE(backend_list); i++)
81                 if (!strcmp(drm_version->name, backend_list[i]->name)) {
82                         drmFreeVersion(drm_version);
83                         return backend_list[i];
84                 }
85
86         drmFreeVersion(drm_version);
87         return NULL;
88 }
89
90 struct driver *drv_create(int fd)
91 {
92         struct driver *drv;
93         int ret;
94
95         drv = (struct driver *) calloc(1, sizeof(*drv));
96
97         if (!drv)
98                 return NULL;
99
100         drv->fd = fd;
101         drv->backend = drv_get_backend(fd);
102
103         if (!drv->backend) {
104                 free(drv);
105                 return NULL;
106         }
107
108         if (pthread_mutex_init(&drv->table_lock, NULL)) {
109                 free(drv);
110                 return NULL;
111         }
112
113         drv->buffer_table = drmHashCreate();
114         if (!drv->buffer_table) {
115                 free(drv);
116                 return NULL;
117         }
118
119         if (drv->backend->init) {
120                 ret = drv->backend->init(drv);
121                 if (ret) {
122                         free(drv);
123                         return NULL;
124                 }
125         }
126
127         return drv;
128 }
129
130 void drv_destroy(struct driver *drv)
131 {
132         if (drv->backend->close)
133                 drv->backend->close(drv);
134
135         pthread_mutex_destroy(&drv->table_lock);
136         drmHashDestroy(drv->buffer_table);
137
138         free(drv);
139 }
140
141 int drv_get_fd(struct driver *drv)
142 {
143         return drv->fd;
144 }
145
146 const char *
147 drv_get_name(struct driver *drv)
148 {
149         return drv->backend->name;
150 }
151
152 int drv_is_format_supported(struct driver *drv, drv_format_t format,
153                             uint64_t usage)
154 {
155         unsigned int i;
156
157         if (format == DRV_FORMAT_NONE || usage == DRV_BO_USE_NONE)
158                 return 0;
159
160         for (i = 0 ; i < ARRAY_SIZE(drv->backend->format_list); i++)
161         {
162                 if (!drv->backend->format_list[i].format)
163                         break;
164
165                 if (drv->backend->format_list[i].format == format &&
166                       (drv->backend->format_list[i].usage & usage) == usage)
167                         return 1;
168         }
169
170         return 0;
171 }
172
173 struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height,
174                       drv_format_t format)
175 {
176
177         struct bo *bo;
178         bo = (struct bo *) calloc(1, sizeof(*bo));
179
180         if (!bo)
181                 return NULL;
182
183         bo->drv = drv;
184         bo->width = width;
185         bo->height = height;
186         bo->format = format;
187         bo->num_planes = drv_num_planes_from_format(format);
188
189         if (!bo->num_planes) {
190                 free(bo);
191                 return NULL;
192         }
193
194         return bo;
195 }
196
197 struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height,
198                          drv_format_t format, uint64_t flags)
199 {
200         int ret;
201         size_t plane;
202         struct bo *bo;
203
204         bo = drv_bo_new(drv, width, height, format);
205
206         if (!bo)
207                 return NULL;
208
209         ret = drv->backend->bo_create(bo, width, height, format, flags);
210
211         if (ret) {
212                 free(bo);
213                 return NULL;
214         }
215
216         pthread_mutex_lock(&drv->table_lock);
217
218         for (plane = 0; plane < bo->num_planes; plane++)
219                 drv_increment_reference_count(drv, bo, plane);
220
221         pthread_mutex_unlock(&drv->table_lock);
222
223         return bo;
224 }
225
226 void drv_bo_destroy(struct bo *bo)
227 {
228         size_t plane;
229         uintptr_t total = 0;
230         struct driver *drv = bo->drv;
231
232         pthread_mutex_lock(&drv->table_lock);
233
234         for (plane = 0; plane < bo->num_planes; plane++)
235                 drv_decrement_reference_count(drv, bo, plane);
236
237         for (plane = 0; plane < bo->num_planes; plane++)
238                 total += drv_get_reference_count(drv, bo, plane);
239
240         pthread_mutex_unlock(&drv->table_lock);
241
242         if (total == 0)
243                 bo->drv->backend->bo_destroy(bo);
244
245         free(bo);
246 }
247
248 void *
249 drv_bo_map(struct bo *bo)
250 {
251         assert(drv_num_buffers_per_bo(bo) == 1);
252
253         if (!bo->map_data || bo->map_data == MAP_FAILED)
254                 bo->map_data = bo->drv->backend->bo_map(bo);
255
256         return bo->map_data;
257 }
258
259 int
260 drv_bo_unmap(struct bo *bo)
261 {
262         int ret = -1;
263
264         assert(drv_num_buffers_per_bo(bo) == 1);
265
266         if (bo->map_data && bo->map_data != MAP_FAILED)
267                 ret = munmap(bo->map_data, bo->sizes[0]);
268
269         bo->map_data = NULL;
270
271         return ret;
272 }
273
274 struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
275 {
276         int ret;
277         struct bo *bo;
278         struct drm_prime_handle prime_handle;
279
280         memset(&prime_handle, 0, sizeof(prime_handle));
281         prime_handle.fd = data->fd;
282
283         /* This function can support only single plane formats. */
284         /* If multi-plane import is desired, new function should be added. */
285         if (drv_num_planes_from_format(data->format) != 1)
286                 return NULL;
287
288         bo = drv_bo_new(drv, data->width, data->height, data->format);
289
290         ret = drmIoctl(drv->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE,
291                        &prime_handle);
292
293         if (ret) {
294                 fprintf(stderr, "drv: DRM_IOCTL_PRIME_FD_TO_HANDLE failed "
295                                 "(fd=%u)\n", prime_handle.fd);
296                 free(bo);
297                 return NULL;
298         }
299
300         bo->strides[0] = data->stride;
301         bo->sizes[0] = data->height * data->stride;
302         bo->handles[0].u32 = prime_handle.handle;
303
304         pthread_mutex_lock(&drv->table_lock);
305         drv_increment_reference_count(drv, bo, 0);
306         pthread_mutex_unlock(&drv->table_lock);
307
308         return bo;
309 }
310
311 uint32_t drv_bo_get_width(struct bo *bo)
312 {
313         return bo->width;
314 }
315
316 uint32_t drv_bo_get_height(struct bo *bo)
317 {
318         return bo->height;
319 }
320
321 uint32_t drv_bo_get_stride_or_tiling(struct bo *bo)
322 {
323         return bo->tiling ? bo->tiling : drv_bo_get_plane_stride(bo, 0);
324 }
325
326 size_t drv_bo_get_num_planes(struct bo *bo)
327 {
328         return bo->num_planes;
329 }
330
331 union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
332 {
333         return bo->handles[plane];
334 }
335
336 #ifndef DRM_RDWR
337 #define DRM_RDWR O_RDWR
338 #endif
339
340 int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
341 {
342
343         int ret, fd;
344         assert(plane < bo->num_planes);
345
346         ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32,
347                                  DRM_CLOEXEC | DRM_RDWR, &fd);
348
349         return (ret) ? ret : fd;
350
351 }
352
353 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
354 {
355         assert(plane < bo->num_planes);
356         return bo->offsets[plane];
357 }
358
359 uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
360 {
361         assert(plane < bo->num_planes);
362         return bo->sizes[plane];
363 }
364
365 uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
366 {
367         assert(plane < bo->num_planes);
368         return bo->strides[plane];
369 }
370
371 uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
372 {
373         assert(plane < bo->num_planes);
374         return bo->format_modifiers[plane];
375 }