OSDN Git Service

minigbm: cros_gralloc: simplify getting the drm format string
[android-x86/external-minigbm.git] / drv.c
1 /*
2  * Copyright 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 <errno.h>
8 #include <fcntl.h>
9 #include <pthread.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 #include <xf86drm.h>
18
19 #ifdef __ANDROID__
20 #include <cutils/log.h>
21 #include <libgen.h>
22 #endif
23
24 #include "drv_priv.h"
25 #include "helpers.h"
26 #include "util.h"
27
28 #ifdef DRV_AMDGPU
29 extern const struct backend backend_amdgpu;
30 #endif
31 extern const struct backend backend_evdi;
32 #ifdef DRV_EXYNOS
33 extern const struct backend backend_exynos;
34 #endif
35 #ifdef DRV_I915
36 extern const struct backend backend_i915;
37 #endif
38 #ifdef DRV_MARVELL
39 extern const struct backend backend_marvell;
40 #endif
41 #ifdef DRV_MEDIATEK
42 extern const struct backend backend_mediatek;
43 #endif
44 #ifdef DRV_MESON
45 extern const struct backend backend_meson;
46 #endif
47 #ifdef DRV_MSM
48 extern const struct backend backend_msm;
49 #endif
50 extern const struct backend backend_nouveau;
51 #ifdef DRV_RADEON
52 extern const struct backend backend_radeon;
53 #endif
54 #ifdef DRV_ROCKCHIP
55 extern const struct backend backend_rockchip;
56 #endif
57 #ifdef DRV_SYNAPTICS
58 extern const struct backend backend_synaptics;
59 #endif
60 #ifdef DRV_TEGRA
61 extern const struct backend backend_tegra;
62 #endif
63 extern const struct backend backend_udl;
64 #ifdef DRV_VC4
65 extern const struct backend backend_vc4;
66 #endif
67 extern const struct backend backend_virtio_gpu;
68
69 static const struct backend *drv_get_backend(int fd)
70 {
71         drmVersionPtr drm_version;
72         unsigned int i;
73
74         drm_version = drmGetVersion(fd);
75
76         if (!drm_version)
77                 return NULL;
78
79         const struct backend *backend_list[] = {
80 #ifdef DRV_AMDGPU
81                 &backend_amdgpu,
82 #endif
83                 &backend_evdi,
84 #ifdef DRV_EXYNOS
85                 &backend_exynos,
86 #endif
87 #ifdef DRV_I915
88                 &backend_i915,
89 #endif
90 #ifdef DRV_MARVELL
91                 &backend_marvell,
92 #endif
93 #ifdef DRV_MEDIATEK
94                 &backend_mediatek,
95 #endif
96 #ifdef DRV_MESON
97                 &backend_meson,
98 #endif
99 #ifdef DRV_MSM
100                 &backend_msm,
101 #endif
102                 &backend_nouveau,
103 #ifdef DRV_RADEON
104                 &backend_radeon,
105 #endif
106 #ifdef DRV_ROCKCHIP
107                 &backend_rockchip,
108 #endif
109 #ifdef DRV_SYNAPTICS
110                 &backend_synaptics,
111 #endif
112 #ifdef DRV_TEGRA
113                 &backend_tegra,
114 #endif
115                 &backend_udl,
116 #ifdef DRV_VC4
117                 &backend_vc4,
118 #endif
119                 &backend_virtio_gpu,
120         };
121
122         for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
123                 const struct backend *b = backend_list[i];
124                 // Exactly one of the main create functions must be defined.
125                 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
126                 // Either both or neither must be implemented.
127                 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
128                 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
129                 assert((b->bo_create_with_modifiers == NULL) ||
130                        (b->bo_create_from_metadata == NULL));
131
132                 if (!strcmp(drm_version->name, b->name)) {
133                         drmFreeVersion(drm_version);
134                         return b;
135                 }
136         }
137
138         drmFreeVersion(drm_version);
139         return NULL;
140 }
141
142 struct driver *drv_create(int fd)
143 {
144         struct driver *drv;
145         int ret;
146
147         drv = (struct driver *)calloc(1, sizeof(*drv));
148
149         if (!drv)
150                 return NULL;
151
152         drv->fd = fd;
153         drv->backend = drv_get_backend(fd);
154
155         if (!drv->backend)
156                 goto free_driver;
157
158         if (pthread_mutex_init(&drv->driver_lock, NULL))
159                 goto free_driver;
160
161         drv->buffer_table = drmHashCreate();
162         if (!drv->buffer_table)
163                 goto free_lock;
164
165         drv->mappings = drv_array_init(sizeof(struct mapping));
166         if (!drv->mappings)
167                 goto free_buffer_table;
168
169         drv->combos = drv_array_init(sizeof(struct combination));
170         if (!drv->combos)
171                 goto free_mappings;
172
173         if (drv->backend->init) {
174                 ret = drv->backend->init(drv);
175                 if (ret) {
176                         drv_array_destroy(drv->combos);
177                         goto free_mappings;
178                 }
179         }
180
181         return drv;
182
183 free_mappings:
184         drv_array_destroy(drv->mappings);
185 free_buffer_table:
186         drmHashDestroy(drv->buffer_table);
187 free_lock:
188         pthread_mutex_destroy(&drv->driver_lock);
189 free_driver:
190         free(drv);
191         return NULL;
192 }
193
194 void drv_destroy(struct driver *drv)
195 {
196         pthread_mutex_lock(&drv->driver_lock);
197
198         if (drv->backend->close)
199                 drv->backend->close(drv);
200
201         drmHashDestroy(drv->buffer_table);
202         drv_array_destroy(drv->mappings);
203         drv_array_destroy(drv->combos);
204
205         pthread_mutex_unlock(&drv->driver_lock);
206         pthread_mutex_destroy(&drv->driver_lock);
207
208         free(drv);
209 }
210
211 int drv_get_fd(struct driver *drv)
212 {
213         return drv->fd;
214 }
215
216 const char *drv_get_name(struct driver *drv)
217 {
218         return drv->backend->name;
219 }
220
221 struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
222 {
223         struct combination *curr, *best;
224
225         if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
226                 return 0;
227
228         best = NULL;
229         uint32_t i;
230         for (i = 0; i < drv_array_size(drv->combos); i++) {
231                 curr = drv_array_at_idx(drv->combos, i);
232                 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
233                         if (!best || best->metadata.priority < curr->metadata.priority)
234                                 best = curr;
235         }
236
237         return best;
238 }
239
240 struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
241                       uint64_t use_flags, bool is_test_buffer)
242 {
243
244         struct bo *bo;
245         bo = (struct bo *)calloc(1, sizeof(*bo));
246
247         if (!bo)
248                 return NULL;
249
250         bo->drv = drv;
251         bo->meta.width = width;
252         bo->meta.height = height;
253         bo->meta.format = format;
254         bo->meta.use_flags = use_flags;
255         bo->meta.num_planes = drv_num_planes_from_format(format);
256         bo->is_test_buffer = is_test_buffer;
257
258         if (!bo->meta.num_planes) {
259                 free(bo);
260                 return NULL;
261         }
262
263         return bo;
264 }
265
266 struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
267                          uint64_t use_flags)
268 {
269         int ret;
270         size_t plane;
271         struct bo *bo;
272         bool is_test_alloc;
273
274         is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
275         use_flags &= ~BO_USE_TEST_ALLOC;
276
277         bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
278
279         if (!bo)
280                 return NULL;
281
282         ret = -EINVAL;
283         if (drv->backend->bo_compute_metadata) {
284                 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
285                                                         0);
286                 if (!is_test_alloc && ret == 0)
287                         ret = drv->backend->bo_create_from_metadata(bo);
288         } else if (!is_test_alloc) {
289                 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
290         }
291
292         if (ret) {
293                 free(bo);
294                 return NULL;
295         }
296
297         pthread_mutex_lock(&drv->driver_lock);
298
299         for (plane = 0; plane < bo->meta.num_planes; plane++) {
300                 if (plane > 0)
301                         assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
302
303                 drv_increment_reference_count(drv, bo, plane);
304         }
305
306         pthread_mutex_unlock(&drv->driver_lock);
307
308         return bo;
309 }
310
311 struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
312                                         uint32_t format, const uint64_t *modifiers, uint32_t count)
313 {
314         int ret;
315         size_t plane;
316         struct bo *bo;
317
318         if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
319                 errno = ENOENT;
320                 return NULL;
321         }
322
323         bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
324
325         if (!bo)
326                 return NULL;
327
328         ret = -EINVAL;
329         if (drv->backend->bo_compute_metadata) {
330                 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
331                                                         modifiers, count);
332                 if (ret == 0)
333                         ret = drv->backend->bo_create_from_metadata(bo);
334         } else {
335                 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
336                                                              count);
337         }
338
339         if (ret) {
340                 free(bo);
341                 return NULL;
342         }
343
344         pthread_mutex_lock(&drv->driver_lock);
345
346         for (plane = 0; plane < bo->meta.num_planes; plane++) {
347                 if (plane > 0)
348                         assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
349
350                 drv_increment_reference_count(drv, bo, plane);
351         }
352
353         pthread_mutex_unlock(&drv->driver_lock);
354
355         return bo;
356 }
357
358 void drv_bo_destroy(struct bo *bo)
359 {
360         int ret;
361         size_t plane;
362         uintptr_t total = 0;
363         struct driver *drv = bo->drv;
364
365         if (!bo->is_test_buffer) {
366                 pthread_mutex_lock(&drv->driver_lock);
367
368                 for (plane = 0; plane < bo->meta.num_planes; plane++)
369                         drv_decrement_reference_count(drv, bo, plane);
370
371                 for (plane = 0; plane < bo->meta.num_planes; plane++)
372                         total += drv_get_reference_count(drv, bo, plane);
373
374                 pthread_mutex_unlock(&drv->driver_lock);
375
376                 if (total == 0) {
377                         ret = drv_mapping_destroy(bo);
378                         assert(ret == 0);
379                         bo->drv->backend->bo_destroy(bo);
380                 }
381         }
382
383         free(bo);
384 }
385
386 struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
387 {
388         int ret;
389         size_t plane;
390         struct bo *bo;
391         off_t seek_end;
392
393         bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
394
395         if (!bo)
396                 return NULL;
397
398         ret = drv->backend->bo_import(bo, data);
399         if (ret) {
400                 free(bo);
401                 return NULL;
402         }
403
404         for (plane = 0; plane < bo->meta.num_planes; plane++) {
405                 pthread_mutex_lock(&bo->drv->driver_lock);
406                 drv_increment_reference_count(bo->drv, bo, plane);
407                 pthread_mutex_unlock(&bo->drv->driver_lock);
408         }
409
410         for (plane = 0; plane < bo->meta.num_planes; plane++) {
411                 bo->meta.strides[plane] = data->strides[plane];
412                 bo->meta.offsets[plane] = data->offsets[plane];
413                 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
414
415                 seek_end = lseek(data->fds[plane], 0, SEEK_END);
416                 if (seek_end == (off_t)(-1)) {
417                         drv_log("lseek() failed with %s\n", strerror(errno));
418                         goto destroy_bo;
419                 }
420
421                 lseek(data->fds[plane], 0, SEEK_SET);
422                 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
423                         bo->meta.sizes[plane] = seek_end - data->offsets[plane];
424                 else
425                         bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
426
427                 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
428                         drv_log("buffer size is too large.\n");
429                         goto destroy_bo;
430                 }
431
432                 bo->meta.total_size += bo->meta.sizes[plane];
433         }
434
435         return bo;
436
437 destroy_bo:
438         drv_bo_destroy(bo);
439         return NULL;
440 }
441
442 void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
443                  struct mapping **map_data, size_t plane)
444 {
445         uint32_t i;
446         uint8_t *addr;
447         struct mapping mapping;
448
449         assert(rect->width >= 0);
450         assert(rect->height >= 0);
451         assert(rect->x + rect->width <= drv_bo_get_width(bo));
452         assert(rect->y + rect->height <= drv_bo_get_height(bo));
453         assert(BO_MAP_READ_WRITE & map_flags);
454         /* No CPU access for protected buffers. */
455         assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
456
457         if (bo->is_test_buffer) {
458                 return MAP_FAILED;
459         }
460
461         memset(&mapping, 0, sizeof(mapping));
462         mapping.rect = *rect;
463         mapping.refcount = 1;
464
465         pthread_mutex_lock(&bo->drv->driver_lock);
466
467         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
468                 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
469                 if (prior->vma->handle != bo->handles[plane].u32 ||
470                     prior->vma->map_flags != map_flags)
471                         continue;
472
473                 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
474                     rect->width != prior->rect.width || rect->height != prior->rect.height)
475                         continue;
476
477                 prior->refcount++;
478                 *map_data = prior;
479                 goto exact_match;
480         }
481
482         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
483                 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
484                 if (prior->vma->handle != bo->handles[plane].u32 ||
485                     prior->vma->map_flags != map_flags)
486                         continue;
487
488                 prior->vma->refcount++;
489                 mapping.vma = prior->vma;
490                 goto success;
491         }
492
493         mapping.vma = calloc(1, sizeof(*mapping.vma));
494         memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
495         addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
496         if (addr == MAP_FAILED) {
497                 *map_data = NULL;
498                 free(mapping.vma);
499                 pthread_mutex_unlock(&bo->drv->driver_lock);
500                 return MAP_FAILED;
501         }
502
503         mapping.vma->refcount = 1;
504         mapping.vma->addr = addr;
505         mapping.vma->handle = bo->handles[plane].u32;
506         mapping.vma->map_flags = map_flags;
507
508 success:
509         *map_data = drv_array_append(bo->drv->mappings, &mapping);
510 exact_match:
511         drv_bo_invalidate(bo, *map_data);
512         addr = (uint8_t *)((*map_data)->vma->addr);
513         addr += drv_bo_get_plane_offset(bo, plane);
514         pthread_mutex_unlock(&bo->drv->driver_lock);
515         return (void *)addr;
516 }
517
518 int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
519 {
520         uint32_t i;
521         int ret = 0;
522
523         pthread_mutex_lock(&bo->drv->driver_lock);
524
525         if (--mapping->refcount)
526                 goto out;
527
528         if (!--mapping->vma->refcount) {
529                 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
530                 free(mapping->vma);
531         }
532
533         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
534                 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
535                         drv_array_remove(bo->drv->mappings, i);
536                         break;
537                 }
538         }
539
540 out:
541         pthread_mutex_unlock(&bo->drv->driver_lock);
542         return ret;
543 }
544
545 int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
546 {
547         int ret = 0;
548
549         assert(mapping);
550         assert(mapping->vma);
551         assert(mapping->refcount > 0);
552         assert(mapping->vma->refcount > 0);
553
554         if (bo->drv->backend->bo_invalidate)
555                 ret = bo->drv->backend->bo_invalidate(bo, mapping);
556
557         return ret;
558 }
559
560 int drv_bo_flush(struct bo *bo, struct mapping *mapping)
561 {
562         int ret = 0;
563
564         assert(mapping);
565         assert(mapping->vma);
566         assert(mapping->refcount > 0);
567         assert(mapping->vma->refcount > 0);
568
569         if (bo->drv->backend->bo_flush)
570                 ret = bo->drv->backend->bo_flush(bo, mapping);
571
572         return ret;
573 }
574
575 int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
576 {
577         int ret = 0;
578
579         assert(mapping);
580         assert(mapping->vma);
581         assert(mapping->refcount > 0);
582         assert(mapping->vma->refcount > 0);
583         assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
584
585         if (bo->drv->backend->bo_flush)
586                 ret = bo->drv->backend->bo_flush(bo, mapping);
587         else
588                 ret = drv_bo_unmap(bo, mapping);
589
590         return ret;
591 }
592
593 uint32_t drv_bo_get_width(struct bo *bo)
594 {
595         return bo->meta.width;
596 }
597
598 uint32_t drv_bo_get_height(struct bo *bo)
599 {
600         return bo->meta.height;
601 }
602
603 size_t drv_bo_get_num_planes(struct bo *bo)
604 {
605         return bo->meta.num_planes;
606 }
607
608 union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
609 {
610         return bo->handles[plane];
611 }
612
613 #ifndef DRM_RDWR
614 #define DRM_RDWR O_RDWR
615 #endif
616
617 int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
618 {
619
620         int ret, fd;
621         assert(plane < bo->meta.num_planes);
622
623         if (bo->is_test_buffer) {
624                 return -EINVAL;
625         }
626
627         ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
628
629         // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
630         if (ret)
631                 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
632
633         return (ret) ? ret : fd;
634 }
635
636 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
637 {
638         assert(plane < bo->meta.num_planes);
639         return bo->meta.offsets[plane];
640 }
641
642 uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
643 {
644         assert(plane < bo->meta.num_planes);
645         return bo->meta.sizes[plane];
646 }
647
648 uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
649 {
650         assert(plane < bo->meta.num_planes);
651         return bo->meta.strides[plane];
652 }
653
654 uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
655 {
656         assert(plane < bo->meta.num_planes);
657         return bo->meta.format_modifiers[plane];
658 }
659
660 uint32_t drv_bo_get_format(struct bo *bo)
661 {
662         return bo->meta.format;
663 }
664
665 size_t drv_bo_get_total_size(struct bo *bo)
666 {
667         return bo->meta.total_size;
668 }
669
670 uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
671 {
672         if (drv->backend->resolve_format)
673                 return drv->backend->resolve_format(drv, format, use_flags);
674
675         return format;
676 }
677
678 uint32_t drv_num_buffers_per_bo(struct bo *bo)
679 {
680         uint32_t count = 0;
681         size_t plane, p;
682
683         if (bo->is_test_buffer) {
684                 return 0;
685         }
686
687         for (plane = 0; plane < bo->meta.num_planes; plane++) {
688                 for (p = 0; p < plane; p++)
689                         if (bo->handles[p].u32 == bo->handles[plane].u32)
690                                 break;
691                 if (p == plane)
692                         count++;
693         }
694
695         return count;
696 }
697
698 void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
699 {
700         char buf[50];
701         snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
702
703         va_list args;
704         va_start(args, format);
705 #ifdef __ANDROID__
706         __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
707 #else
708         fprintf(stderr, "%s ", buf);
709         vfprintf(stderr, format, args);
710 #endif
711         va_end(args);
712 }
713
714 int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
715                       uint32_t offsets[DRV_MAX_PLANES])
716 {
717         for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
718                 strides[plane] = bo->meta.strides[plane];
719                 offsets[plane] = bo->meta.offsets[plane];
720         }
721
722         if (bo->drv->backend->resource_info)
723                 return bo->drv->backend->resource_info(bo, strides, offsets);
724
725         return 0;
726 }