OSDN Git Service

virtgpu: enable blobs for decoder output buffers
[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 = { 0 };
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         mapping.rect = *rect;
462         mapping.refcount = 1;
463
464         pthread_mutex_lock(&bo->drv->driver_lock);
465
466         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
467                 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
468                 if (prior->vma->handle != bo->handles[plane].u32 ||
469                     prior->vma->map_flags != map_flags)
470                         continue;
471
472                 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
473                     rect->width != prior->rect.width || rect->height != prior->rect.height)
474                         continue;
475
476                 prior->refcount++;
477                 *map_data = prior;
478                 goto exact_match;
479         }
480
481         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
482                 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
483                 if (prior->vma->handle != bo->handles[plane].u32 ||
484                     prior->vma->map_flags != map_flags)
485                         continue;
486
487                 prior->vma->refcount++;
488                 mapping.vma = prior->vma;
489                 goto success;
490         }
491
492         mapping.vma = calloc(1, sizeof(*mapping.vma));
493         memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
494         addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
495         if (addr == MAP_FAILED) {
496                 *map_data = NULL;
497                 free(mapping.vma);
498                 pthread_mutex_unlock(&bo->drv->driver_lock);
499                 return MAP_FAILED;
500         }
501
502         mapping.vma->refcount = 1;
503         mapping.vma->addr = addr;
504         mapping.vma->handle = bo->handles[plane].u32;
505         mapping.vma->map_flags = map_flags;
506
507 success:
508         *map_data = drv_array_append(bo->drv->mappings, &mapping);
509 exact_match:
510         drv_bo_invalidate(bo, *map_data);
511         addr = (uint8_t *)((*map_data)->vma->addr);
512         addr += drv_bo_get_plane_offset(bo, plane);
513         pthread_mutex_unlock(&bo->drv->driver_lock);
514         return (void *)addr;
515 }
516
517 int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
518 {
519         uint32_t i;
520         int ret = 0;
521
522         pthread_mutex_lock(&bo->drv->driver_lock);
523
524         if (--mapping->refcount)
525                 goto out;
526
527         if (!--mapping->vma->refcount) {
528                 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
529                 free(mapping->vma);
530         }
531
532         for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
533                 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
534                         drv_array_remove(bo->drv->mappings, i);
535                         break;
536                 }
537         }
538
539 out:
540         pthread_mutex_unlock(&bo->drv->driver_lock);
541         return ret;
542 }
543
544 int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
545 {
546         int ret = 0;
547
548         assert(mapping);
549         assert(mapping->vma);
550         assert(mapping->refcount > 0);
551         assert(mapping->vma->refcount > 0);
552
553         if (bo->drv->backend->bo_invalidate)
554                 ret = bo->drv->backend->bo_invalidate(bo, mapping);
555
556         return ret;
557 }
558
559 int drv_bo_flush(struct bo *bo, struct mapping *mapping)
560 {
561         int ret = 0;
562
563         assert(mapping);
564         assert(mapping->vma);
565         assert(mapping->refcount > 0);
566         assert(mapping->vma->refcount > 0);
567
568         if (bo->drv->backend->bo_flush)
569                 ret = bo->drv->backend->bo_flush(bo, mapping);
570
571         return ret;
572 }
573
574 int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
575 {
576         int ret = 0;
577
578         assert(mapping);
579         assert(mapping->vma);
580         assert(mapping->refcount > 0);
581         assert(mapping->vma->refcount > 0);
582         assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
583
584         if (bo->drv->backend->bo_flush)
585                 ret = bo->drv->backend->bo_flush(bo, mapping);
586         else
587                 ret = drv_bo_unmap(bo, mapping);
588
589         return ret;
590 }
591
592 uint32_t drv_bo_get_width(struct bo *bo)
593 {
594         return bo->meta.width;
595 }
596
597 uint32_t drv_bo_get_height(struct bo *bo)
598 {
599         return bo->meta.height;
600 }
601
602 size_t drv_bo_get_num_planes(struct bo *bo)
603 {
604         return bo->meta.num_planes;
605 }
606
607 union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
608 {
609         return bo->handles[plane];
610 }
611
612 #ifndef DRM_RDWR
613 #define DRM_RDWR O_RDWR
614 #endif
615
616 int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
617 {
618
619         int ret, fd;
620         assert(plane < bo->meta.num_planes);
621
622         if (bo->is_test_buffer) {
623                 return -EINVAL;
624         }
625
626         ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
627
628         // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
629         if (ret)
630                 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
631
632         return (ret) ? ret : fd;
633 }
634
635 uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
636 {
637         assert(plane < bo->meta.num_planes);
638         return bo->meta.offsets[plane];
639 }
640
641 uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
642 {
643         assert(plane < bo->meta.num_planes);
644         return bo->meta.sizes[plane];
645 }
646
647 uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
648 {
649         assert(plane < bo->meta.num_planes);
650         return bo->meta.strides[plane];
651 }
652
653 uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
654 {
655         assert(plane < bo->meta.num_planes);
656         return bo->meta.format_modifiers[plane];
657 }
658
659 uint32_t drv_bo_get_format(struct bo *bo)
660 {
661         return bo->meta.format;
662 }
663
664 size_t drv_bo_get_total_size(struct bo *bo)
665 {
666         return bo->meta.total_size;
667 }
668
669 uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
670 {
671         if (drv->backend->resolve_format)
672                 return drv->backend->resolve_format(drv, format, use_flags);
673
674         return format;
675 }
676
677 uint32_t drv_num_buffers_per_bo(struct bo *bo)
678 {
679         uint32_t count = 0;
680         size_t plane, p;
681
682         if (bo->is_test_buffer) {
683                 return 0;
684         }
685
686         for (plane = 0; plane < bo->meta.num_planes; plane++) {
687                 for (p = 0; p < plane; p++)
688                         if (bo->handles[p].u32 == bo->handles[plane].u32)
689                                 break;
690                 if (p == plane)
691                         count++;
692         }
693
694         return count;
695 }
696
697 void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
698 {
699         char buf[50];
700         snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
701
702         va_list args;
703         va_start(args, format);
704 #ifdef __ANDROID__
705         __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
706 #else
707         fprintf(stderr, "%s ", buf);
708         vfprintf(stderr, format, args);
709 #endif
710         va_end(args);
711 }
712
713 int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
714                       uint32_t offsets[DRV_MAX_PLANES])
715 {
716         for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
717                 strides[plane] = bo->meta.strides[plane];
718                 offsets[plane] = bo->meta.offsets[plane];
719         }
720
721         if (bo->drv->backend->resource_info)
722                 return bo->drv->backend->resource_info(bo, strides, offsets);
723
724         return 0;
725 }