blob: 8db5a5d7d13d9934cdfb9d692108be77637525a4 [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
Leona Chou8f708a12020-03-19 15:06:12 +080057#ifdef DRV_SYNAPTICS
58extern const struct backend backend_synaptics;
59#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070060#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070061extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070062#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070063extern const struct backend backend_udl;
Niklas Schulze878fed42017-02-08 15:29:21 +010064#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010066#endif
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070067extern const struct backend backend_virtio_gpu;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070068
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070069static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070070{
71 drmVersionPtr drm_version;
72 unsigned int i;
73
74 drm_version = drmGetVersion(fd);
75
76 if (!drm_version)
77 return NULL;
78
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070079 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053080#ifdef DRV_AMDGPU
81 &backend_amdgpu,
82#endif
Gurchetan Singh29ed8d22017-10-31 10:39:43 -070083 &backend_evdi,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070084#ifdef DRV_EXYNOS
85 &backend_exynos,
86#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070087#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
Sergey Volk6eca3682018-03-06 13:29:32 -080096#ifdef DRV_MESON
97 &backend_meson,
98#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053099#ifdef DRV_MSM
100 &backend_msm,
101#endif
Daniele Castagna71db2b52016-12-16 16:24:06 -0500102 &backend_nouveau,
giri3f259512017-08-02 12:01:33 -0400103#ifdef DRV_RADEON
104 &backend_radeon,
105#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700106#ifdef DRV_ROCKCHIP
107 &backend_rockchip,
108#endif
Leona Chou8f708a12020-03-19 15:06:12 +0800109#ifdef DRV_SYNAPTICS
110 &backend_synaptics,
111#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700112#ifdef DRV_TEGRA
113 &backend_tegra,
114#endif
115 &backend_udl,
Niklas Schulze878fed42017-02-08 15:29:21 +0100116#ifdef DRV_VC4
117 &backend_vc4,
118#endif
Gurchetan Singhec9bbc22019-12-19 10:01:13 -0800119 &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700120 };
121
David Stevens26fe6822020-03-09 12:23:42 +0000122 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)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700133 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000134 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700135 }
David Stevens26fe6822020-03-09 12:23:42 +0000136 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700137
138 drmFreeVersion(drm_version);
139 return NULL;
140}
141
142struct driver *drv_create(int fd)
143{
144 struct driver *drv;
145 int ret;
146
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800147 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700148
149 if (!drv)
150 return NULL;
151
152 drv->fd = fd;
153 drv->backend = drv_get_backend(fd);
154
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700155 if (!drv->backend)
156 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700157
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800158 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700159 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700160
161 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162 if (!drv->buffer_table)
163 goto free_lock;
164
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700165 drv->mappings = drv_array_init(sizeof(struct mapping));
166 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700167 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700168
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700169 drv->combos = drv_array_init(sizeof(struct combination));
170 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700171 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700172
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700173 if (drv->backend->init) {
174 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800175 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700176 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700177 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800178 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700179 }
180
181 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700182
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700183free_mappings:
184 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700185free_buffer_table:
186 drmHashDestroy(drv->buffer_table);
187free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800188 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700189free_driver:
190 free(drv);
191 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192}
193
194void drv_destroy(struct driver *drv)
195{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800196 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700197
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700198 if (drv->backend->close)
199 drv->backend->close(drv);
200
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700201 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700202 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700203 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700204
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800205 pthread_mutex_unlock(&drv->driver_lock);
206 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700207
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208 free(drv);
209}
210
211int drv_get_fd(struct driver *drv)
212{
213 return drv->fd;
214}
215
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800216const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217{
218 return drv->backend->name;
219}
220
Gurchetan Singha1892b22017-09-28 16:40:52 -0700221struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800223 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700224
Gurchetan Singha1892b22017-09-28 16:40:52 -0700225 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700226 return 0;
227
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800228 best = NULL;
229 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700230 for (i = 0; i < drv_array_size(drv->combos); i++) {
231 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700232 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800233 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800234 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235 }
236
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800237 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700238}
239
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700240struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000241 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242{
243
244 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800245 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246
247 if (!bo)
248 return NULL;
249
250 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700251 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);
David Stevens26fe6822020-03-09 12:23:42 +0000256 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700257
Gurchetan Singh298b7572019-09-19 09:55:18 -0700258 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700259 free(bo);
260 return NULL;
261 }
262
263 return bo;
264}
265
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800266struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700267 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700268{
269 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700270 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700271 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000272 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700273
David Stevens26fe6822020-03-09 12:23:42 +0000274 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);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700278
279 if (!bo)
280 return NULL;
281
David Stevens26fe6822020-03-09 12:23:42 +0000282 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 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700291
292 if (ret) {
293 free(bo);
294 return NULL;
295 }
296
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800297 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700298
Gurchetan Singh298b7572019-09-19 09:55:18 -0700299 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700300 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700301 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700302
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700303 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700304 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700305
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800306 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700307
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700308 return bo;
309}
310
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800311struct 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)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700313{
314 int ret;
315 size_t plane;
316 struct bo *bo;
317
David Stevens26fe6822020-03-09 12:23:42 +0000318 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700319 errno = ENOENT;
320 return NULL;
321 }
322
David Stevens26fe6822020-03-09 12:23:42 +0000323 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700324
325 if (!bo)
326 return NULL;
327
David Stevens26fe6822020-03-09 12:23:42 +0000328 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 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700338
339 if (ret) {
340 free(bo);
341 return NULL;
342 }
343
344 pthread_mutex_lock(&drv->driver_lock);
345
Gurchetan Singh298b7572019-09-19 09:55:18 -0700346 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700347 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700348 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700349
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700350 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700351 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700352
353 pthread_mutex_unlock(&drv->driver_lock);
354
355 return bo;
356}
357
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700358void drv_bo_destroy(struct bo *bo)
359{
Jason Macnak0907d822019-11-12 10:53:05 -0800360 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700361 size_t plane;
362 uintptr_t total = 0;
363 struct driver *drv = bo->drv;
364
David Stevens26fe6822020-03-09 12:23:42 +0000365 if (!bo->is_test_buffer) {
366 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700367
David Stevens26fe6822020-03-09 12:23:42 +0000368 for (plane = 0; plane < bo->meta.num_planes; plane++)
369 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700370
David Stevens26fe6822020-03-09 12:23:42 +0000371 for (plane = 0; plane < bo->meta.num_planes; plane++)
372 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700373
David Stevens26fe6822020-03-09 12:23:42 +0000374 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700375
David Stevens26fe6822020-03-09 12:23:42 +0000376 if (total == 0) {
377 ret = drv_mapping_destroy(bo);
378 assert(ret == 0);
379 bo->drv->backend->bo_destroy(bo);
380 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900381 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700382
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700383 free(bo);
384}
385
386struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
387{
388 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700389 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700390 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700391 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700392
David Stevens26fe6822020-03-09 12:23:42 +0000393 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700394
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700395 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700396 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700397
Gurchetan Singh71611d62017-01-03 16:49:56 -0800398 ret = drv->backend->bo_import(bo, data);
399 if (ret) {
400 free(bo);
401 return NULL;
402 }
403
Gurchetan Singh298b7572019-09-19 09:55:18 -0700404 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530405 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
Gurchetan Singh298b7572019-09-19 09:55:18 -0700410 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];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700414
415 seek_end = lseek(data->fds[plane], 0, SEEK_END);
416 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700417 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700418 goto destroy_bo;
419 }
420
421 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700422 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
423 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700424 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700425 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700426
Gurchetan Singh298b7572019-09-19 09:55:18 -0700427 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700428 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700429 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800430 }
431
Gurchetan Singh298b7572019-09-19 09:55:18 -0700432 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700433 }
434
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700435 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700436
437destroy_bo:
438 drv_bo_destroy(bo);
439 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700440}
441
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800442void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
443 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700444{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700445 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700446 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700447 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700448
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800449 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));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700453 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900454 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700455 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700456
David Stevens26fe6822020-03-09 12:23:42 +0000457 if (bo->is_test_buffer) {
458 return MAP_FAILED;
459 }
460
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800461 mapping.rect = *rect;
462 mapping.refcount = 1;
463
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800464 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700465
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700466 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
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800472 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
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700487 prior->vma->refcount++;
488 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700489 goto success;
490 }
491
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700492 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700493 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800494 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495 if (addr == MAP_FAILED) {
496 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700497 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800498 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700499 return MAP_FAILED;
500 }
501
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700502 mapping.vma->refcount = 1;
503 mapping.vma->addr = addr;
504 mapping.vma->handle = bo->handles[plane].u32;
505 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700506
507success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700508 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800509exact_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);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800513 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800514 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700515}
516
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700517int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700518{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700519 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700520 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700521
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800522 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700523
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800524 if (--mapping->refcount)
525 goto out;
526
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700527 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800528 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700529 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700530 }
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 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700537 }
538
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800539out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800540 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700541 return ret;
542}
543
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700544int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700545{
546 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700547
548 assert(mapping);
549 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800550 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700551 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700552
553 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700554 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700555
556 return ret;
557}
558
Jason Macnak1de7f662020-01-24 15:05:57 -0800559int 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
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700574int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700575{
576 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700577
578 assert(mapping);
579 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800580 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700581 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700582 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700583
584 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700585 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700586 else
587 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700588
589 return ret;
590}
591
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700592uint32_t drv_bo_get_width(struct bo *bo)
593{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700594 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700595}
596
597uint32_t drv_bo_get_height(struct bo *bo)
598{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700599 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700600}
601
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700602size_t drv_bo_get_num_planes(struct bo *bo)
603{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700604 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700605}
606
607union 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
616int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
617{
618
619 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700620 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700621
David Stevens26fe6822020-03-09 12:23:42 +0000622 if (bo->is_test_buffer) {
623 return -EINVAL;
624 }
625
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800626 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700627
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700628 // 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
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700632 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700633}
634
635uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
636{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700637 assert(plane < bo->meta.num_planes);
638 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700639}
640
641uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
642{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700643 assert(plane < bo->meta.num_planes);
644 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700645}
646
647uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
648{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700649 assert(plane < bo->meta.num_planes);
650 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700651}
652
653uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
654{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700655 assert(plane < bo->meta.num_planes);
656 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700657}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700658
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800659uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700660{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700661 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700662}
663
Jason Macnak1de7f662020-01-24 15:05:57 -0800664size_t drv_bo_get_total_size(struct bo *bo)
665{
666 return bo->meta.total_size;
667}
668
Gurchetan Singha1892b22017-09-28 16:40:52 -0700669uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700670{
671 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700672 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700673
674 return format;
675}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700676
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700677uint32_t drv_num_buffers_per_bo(struct bo *bo)
678{
679 uint32_t count = 0;
680 size_t plane, p;
681
David Stevens26fe6822020-03-09 12:23:42 +0000682 if (bo->is_test_buffer) {
683 return 0;
684 }
685
Gurchetan Singh298b7572019-09-19 09:55:18 -0700686 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700687 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}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700696
697void 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}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700712
713int 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}