OSDN Git Service

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