blob: 52f26a51e9866398ab14a2497f986b79e33b0021 [file] [log] [blame]
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07001/*
Daniele Castagna7a755de2016-12-16 17:32:30 -05002 * Copyright 2016 The Chromium OS Authors. All rights reserved.
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07003 * 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>
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -07007#include <errno.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -07008#include <fcntl.h>
Gurchetan Singh1647fbe2016-08-03 17:14:55 -07009#include <pthread.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070010#include <stdint.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
Gurchetan Singhef920532016-08-12 16:38:25 -070014#include <sys/mman.h>
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +080015#include <sys/types.h>
16#include <unistd.h>
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070017#include <xf86drm.h>
18
Alistair Strachan0cfaaa52018-03-19 14:03:23 -070019#ifdef __ANDROID__
20#include <cutils/log.h>
21#include <libgen.h>
22#endif
23
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070024#include "drv_priv.h"
25#include "helpers.h"
26#include "util.h"
27
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053028#ifdef DRV_AMDGPU
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070029extern const struct backend backend_amdgpu;
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053030#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070031extern const struct backend backend_evdi;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070032#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070033extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070034#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070035#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070036extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070037#endif
38#ifdef DRV_MARVELL
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070039extern const struct backend backend_marvell;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070040#endif
41#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070042extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070043#endif
Sergey Volk6eca3682018-03-06 13:29:32 -080044#ifdef DRV_MESON
45extern const struct backend backend_meson;
46#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053047#ifdef DRV_MSM
48extern const struct backend backend_msm;
49#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_nouveau;
giri3f259512017-08-02 12:01:33 -040051#ifdef DRV_RADEON
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070052extern const struct backend backend_radeon;
giri3f259512017-08-02 12:01:33 -040053#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070054#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070055extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070056#endif
57#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070058extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070059#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070060extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010061#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070062extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010063#endif
Kazuhiro Inaba5b91ec02019-09-19 03:18:31 +000064extern const struct backend backend_vgem;
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070066
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070068{
69 drmVersionPtr drm_version;
70 unsigned int i;
71
72 drm_version = drmGetVersion(fd);
73
74 if (!drm_version)
75 return NULL;
76
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070077 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053078#ifdef DRV_AMDGPU
79 &backend_amdgpu,
80#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070081 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_EXYNOS
83 &backend_exynos,
84#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070085#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
Sergey Volk6eca3682018-03-06 13:29:32 -080094#ifdef DRV_MESON
95 &backend_meson,
96#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053097#ifdef DRV_MSM
98 &backend_msm,
99#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -0500100 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -0400101#ifdef DRV_RADEON
102 &backend_radeon,
103#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104#ifdef DRV_ROCKCHIP
105 &backend_rockchip,
106#endif
107#ifdef DRV_TEGRA
108 &backend_tegra,
109#endif
110 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100111#ifdef DRV_VC4
112 &backend_vc4,
113#endif
Junichi Uekawad441f4e2020-01-29 15:45:16 +0900114 &backend_vgem, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 };
116
David Stevens26fe6822020-03-09 12:23:42 +0000117 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
118 const struct backend *b = backend_list[i];
119 // Exactly one of the main create functions must be defined.
120 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
121 // Either both or neither must be implemented.
122 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
123 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
124 assert((b->bo_create_with_modifiers == NULL) ||
125 (b->bo_create_from_metadata == NULL));
126
127 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000129 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130 }
David Stevens26fe6822020-03-09 12:23:42 +0000131 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700132
133 drmFreeVersion(drm_version);
134 return NULL;
135}
136
137struct driver *drv_create(int fd)
138{
139 struct driver *drv;
140 int ret;
141
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800142 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700143
144 if (!drv)
145 return NULL;
146
147 drv->fd = fd;
148 drv->backend = drv_get_backend(fd);
149
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700150 if (!drv->backend)
151 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800153 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700154 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700155
156 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700157 if (!drv->buffer_table)
158 goto free_lock;
159
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700160 drv->mappings = drv_array_init(sizeof(struct mapping));
161 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700163
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700164 drv->combos = drv_array_init(sizeof(struct combination));
165 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700166 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700167
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700168 if (drv->backend->init) {
169 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800170 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700171 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700172 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800173 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700174 }
175
176 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700177
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700178free_mappings:
179 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700180free_buffer_table:
181 drmHashDestroy(drv->buffer_table);
182free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800183 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700184free_driver:
185 free(drv);
186 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700187}
188
189void drv_destroy(struct driver *drv)
190{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800191 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700192
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700193 if (drv->backend->close)
194 drv->backend->close(drv);
195
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700196 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700197 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700198 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700199
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800200 pthread_mutex_unlock(&drv->driver_lock);
201 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700202
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700203 free(drv);
204}
205
206int drv_get_fd(struct driver *drv)
207{
208 return drv->fd;
209}
210
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800211const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700212{
213 return drv->backend->name;
214}
215
Gurchetan Singha1892b22017-09-28 16:40:52 -0700216struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800218 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219
Gurchetan Singha1892b22017-09-28 16:40:52 -0700220 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221 return 0;
222
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800223 best = NULL;
224 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700225 for (i = 0; i < drv_array_size(drv->combos); i++) {
226 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700227 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800228 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800229 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230 }
231
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800232 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700233}
234
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700235struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000236 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237{
238
239 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800240 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241
242 if (!bo)
243 return NULL;
244
245 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700246 bo->meta.width = width;
247 bo->meta.height = height;
248 bo->meta.format = format;
249 bo->meta.use_flags = use_flags;
250 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000251 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252
Gurchetan Singh298b7572019-09-19 09:55:18 -0700253 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254 free(bo);
255 return NULL;
256 }
257
258 return bo;
259}
260
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800261struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700262 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700263{
264 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700265 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700266 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000267 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700268
David Stevens26fe6822020-03-09 12:23:42 +0000269 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
270 use_flags &= ~BO_USE_TEST_ALLOC;
271
272 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700273
274 if (!bo)
275 return NULL;
276
David Stevens26fe6822020-03-09 12:23:42 +0000277 ret = -EINVAL;
278 if (drv->backend->bo_compute_metadata) {
279 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
280 0);
281 if (!is_test_alloc && ret == 0)
282 ret = drv->backend->bo_create_from_metadata(bo);
283 } else if (!is_test_alloc) {
284 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
285 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700286
287 if (ret) {
288 free(bo);
289 return NULL;
290 }
291
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800292 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700293
Gurchetan Singh298b7572019-09-19 09:55:18 -0700294 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700295 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700296 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700297
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700298 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700299 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700300
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800301 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700302
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700303 return bo;
304}
305
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800306struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
307 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700308{
309 int ret;
310 size_t plane;
311 struct bo *bo;
312
David Stevens26fe6822020-03-09 12:23:42 +0000313 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700314 errno = ENOENT;
315 return NULL;
316 }
317
David Stevens26fe6822020-03-09 12:23:42 +0000318 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700319
320 if (!bo)
321 return NULL;
322
David Stevens26fe6822020-03-09 12:23:42 +0000323 ret = -EINVAL;
324 if (drv->backend->bo_compute_metadata) {
325 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
326 modifiers, count);
327 if (ret == 0)
328 ret = drv->backend->bo_create_from_metadata(bo);
329 } else {
330 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
331 count);
332 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700333
334 if (ret) {
335 free(bo);
336 return NULL;
337 }
338
339 pthread_mutex_lock(&drv->driver_lock);
340
Gurchetan Singh298b7572019-09-19 09:55:18 -0700341 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700342 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700343 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700344
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700345 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700346 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700347
348 pthread_mutex_unlock(&drv->driver_lock);
349
350 return bo;
351}
352
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700353void drv_bo_destroy(struct bo *bo)
354{
Jason Macnak0907d822019-11-12 10:53:05 -0800355 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700356 size_t plane;
357 uintptr_t total = 0;
358 struct driver *drv = bo->drv;
359
David Stevens26fe6822020-03-09 12:23:42 +0000360 if (!bo->is_test_buffer) {
361 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700362
David Stevens26fe6822020-03-09 12:23:42 +0000363 for (plane = 0; plane < bo->meta.num_planes; plane++)
364 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700365
David Stevens26fe6822020-03-09 12:23:42 +0000366 for (plane = 0; plane < bo->meta.num_planes; plane++)
367 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700368
David Stevens26fe6822020-03-09 12:23:42 +0000369 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700370
David Stevens26fe6822020-03-09 12:23:42 +0000371 if (total == 0) {
372 ret = drv_mapping_destroy(bo);
373 assert(ret == 0);
374 bo->drv->backend->bo_destroy(bo);
375 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900376 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700377
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700378 free(bo);
379}
380
381struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
382{
383 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700384 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700385 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700386 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700387
David Stevens26fe6822020-03-09 12:23:42 +0000388 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700389
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700390 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700391 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700392
Gurchetan Singh71611d62017-01-03 16:49:56 -0800393 ret = drv->backend->bo_import(bo, data);
394 if (ret) {
395 free(bo);
396 return NULL;
397 }
398
Gurchetan Singh298b7572019-09-19 09:55:18 -0700399 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530400 pthread_mutex_lock(&bo->drv->driver_lock);
401 drv_increment_reference_count(bo->drv, bo, plane);
402 pthread_mutex_unlock(&bo->drv->driver_lock);
403 }
404
Gurchetan Singh298b7572019-09-19 09:55:18 -0700405 for (plane = 0; plane < bo->meta.num_planes; plane++) {
406 bo->meta.strides[plane] = data->strides[plane];
407 bo->meta.offsets[plane] = data->offsets[plane];
408 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700409
410 seek_end = lseek(data->fds[plane], 0, SEEK_END);
411 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700412 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700413 goto destroy_bo;
414 }
415
416 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700417 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
418 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700419 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700420 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700421
Gurchetan Singh298b7572019-09-19 09:55:18 -0700422 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700423 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700424 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800425 }
426
Gurchetan Singh298b7572019-09-19 09:55:18 -0700427 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700428 }
429
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700430 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700431
432destroy_bo:
433 drv_bo_destroy(bo);
434 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700435}
436
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800437void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
438 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700440 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700441 uint8_t *addr;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700442 struct mapping mapping;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700443
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800444 assert(rect->width >= 0);
445 assert(rect->height >= 0);
446 assert(rect->x + rect->width <= drv_bo_get_width(bo));
447 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700448 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900449 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700450 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700451
David Stevens26fe6822020-03-09 12:23:42 +0000452 if (bo->is_test_buffer) {
453 return MAP_FAILED;
454 }
455
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700456 memset(&mapping, 0, sizeof(mapping));
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800457 mapping.rect = *rect;
458 mapping.refcount = 1;
459
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800460 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700461
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700462 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
463 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
464 if (prior->vma->handle != bo->handles[plane].u32 ||
465 prior->vma->map_flags != map_flags)
466 continue;
467
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800468 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
469 rect->width != prior->rect.width || rect->height != prior->rect.height)
470 continue;
471
472 prior->refcount++;
473 *map_data = prior;
474 goto exact_match;
475 }
476
477 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
478 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
479 if (prior->vma->handle != bo->handles[plane].u32 ||
480 prior->vma->map_flags != map_flags)
481 continue;
482
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700483 prior->vma->refcount++;
484 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700485 goto success;
486 }
487
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700488 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700489 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800490 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700491 if (addr == MAP_FAILED) {
492 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700493 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800494 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495 return MAP_FAILED;
496 }
497
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700498 mapping.vma->refcount = 1;
499 mapping.vma->addr = addr;
500 mapping.vma->handle = bo->handles[plane].u32;
501 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700502
503success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700504 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800505exact_match:
506 drv_bo_invalidate(bo, *map_data);
507 addr = (uint8_t *)((*map_data)->vma->addr);
508 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800509 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800510 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700511}
512
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700513int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700514{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700515 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700516 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700517
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800518 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700519
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800520 if (--mapping->refcount)
521 goto out;
522
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700523 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800524 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700525 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700526 }
527
528 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
529 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
530 drv_array_remove(bo->drv->mappings, i);
531 break;
532 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700533 }
534
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800535out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800536 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700537 return ret;
538}
539
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700540int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700541{
542 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700543
544 assert(mapping);
545 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800546 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700547 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700548
549 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700550 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700551
552 return ret;
553}
554
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700555int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700556{
557 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700558
559 assert(mapping);
560 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800561 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700562 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700563 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700564
565 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700566 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700567 else
568 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700569
570 return ret;
571}
572
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700573uint32_t drv_bo_get_width(struct bo *bo)
574{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700575 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700576}
577
578uint32_t drv_bo_get_height(struct bo *bo)
579{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700580 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700581}
582
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700583size_t drv_bo_get_num_planes(struct bo *bo)
584{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700585 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700586}
587
588union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
589{
590 return bo->handles[plane];
591}
592
593#ifndef DRM_RDWR
594#define DRM_RDWR O_RDWR
595#endif
596
597int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
598{
599
600 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700601 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700602
David Stevens26fe6822020-03-09 12:23:42 +0000603 if (bo->is_test_buffer) {
604 return -EINVAL;
605 }
606
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800607 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700608
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700609 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
610 if (ret)
611 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
612
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700613 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700614}
615
616uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
617{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700618 assert(plane < bo->meta.num_planes);
619 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700620}
621
622uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
623{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700624 assert(plane < bo->meta.num_planes);
625 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700626}
627
628uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
629{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700630 assert(plane < bo->meta.num_planes);
631 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700632}
633
634uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
635{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700636 assert(plane < bo->meta.num_planes);
637 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700638}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700639
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800640uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700641{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700642 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700643}
644
Gurchetan Singha1892b22017-09-28 16:40:52 -0700645uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700646{
647 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700648 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700649
650 return format;
651}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700652
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700653uint32_t drv_num_buffers_per_bo(struct bo *bo)
654{
655 uint32_t count = 0;
656 size_t plane, p;
657
David Stevens26fe6822020-03-09 12:23:42 +0000658 if (bo->is_test_buffer) {
659 return 0;
660 }
661
Gurchetan Singh298b7572019-09-19 09:55:18 -0700662 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700663 for (p = 0; p < plane; p++)
664 if (bo->handles[p].u32 == bo->handles[plane].u32)
665 break;
666 if (p == plane)
667 count++;
668 }
669
670 return count;
671}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700672
673void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
674{
675 char buf[50];
676 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
677
678 va_list args;
679 va_start(args, format);
680#ifdef __ANDROID__
681 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
682#else
683 fprintf(stderr, "%s ", buf);
684 vfprintf(stderr, format, args);
685#endif
686 va_end(args);
687}