OSDN Git Service

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