blob: 78e70d7cdadfa1fdf9abb6987acf397d86a62385 [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 Singh46faf6b2016-08-05 14:40:07 -070031#ifdef DRV_EXYNOS
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070032extern const struct backend backend_exynos;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070033#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070034#ifdef DRV_I915
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070035extern const struct backend backend_i915;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070036#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070037#ifdef DRV_MEDIATEK
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070038extern const struct backend backend_mediatek;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070039#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053040#ifdef DRV_MSM
41extern const struct backend backend_msm;
42#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070043#ifdef DRV_ROCKCHIP
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070044extern const struct backend backend_rockchip;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070045#endif
46#ifdef DRV_TEGRA
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_tegra;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070048#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010049#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070050extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010051#endif
Anders Delliene5bef532020-06-10 10:30:44 +010052
53// Dumb / generic drivers
54extern const struct backend backend_evdi;
55extern const struct backend backend_marvell;
56extern const struct backend backend_meson;
57extern const struct backend backend_nouveau;
58extern const struct backend backend_komeda;
59extern const struct backend backend_radeon;
60extern const struct backend backend_synaptics;
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070061extern const struct backend backend_virtio_gpu;
Anders Delliene5bef532020-06-10 10:30:44 +010062extern const struct backend backend_udl;
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040063extern const struct backend backend_vkms;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070064
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070065static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070066{
67 drmVersionPtr drm_version;
68 unsigned int i;
69
70 drm_version = drmGetVersion(fd);
71
72 if (!drm_version)
73 return NULL;
74
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070075 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053076#ifdef DRV_AMDGPU
77 &backend_amdgpu,
78#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070079#ifdef DRV_EXYNOS
80 &backend_exynos,
81#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_I915
83 &backend_i915,
84#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070085#ifdef DRV_MEDIATEK
86 &backend_mediatek,
87#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053088#ifdef DRV_MSM
89 &backend_msm,
90#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070091#ifdef DRV_ROCKCHIP
92 &backend_rockchip,
93#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010094#ifdef DRV_VC4
95 &backend_vc4,
96#endif
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040097 &backend_evdi, &backend_marvell, &backend_meson, &backend_nouveau,
98 &backend_komeda, &backend_radeon, &backend_synaptics, &backend_virtio_gpu,
99 &backend_udl, &backend_virtio_gpu, &backend_vkms
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700100 };
101
David Stevens26fe6822020-03-09 12:23:42 +0000102 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
103 const struct backend *b = backend_list[i];
104 // Exactly one of the main create functions must be defined.
105 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
106 // Either both or neither must be implemented.
107 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
108 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
109 assert((b->bo_create_with_modifiers == NULL) ||
110 (b->bo_create_from_metadata == NULL));
111
112 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700113 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000114 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115 }
David Stevens26fe6822020-03-09 12:23:42 +0000116 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700117
118 drmFreeVersion(drm_version);
119 return NULL;
120}
121
122struct driver *drv_create(int fd)
123{
124 struct driver *drv;
125 int ret;
126
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800127 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700128
129 if (!drv)
130 return NULL;
131
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500132 char *minigbm_debug;
133 minigbm_debug = getenv("MINIGBM_DEBUG");
134 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
135
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700136 drv->fd = fd;
137 drv->backend = drv_get_backend(fd);
138
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700139 if (!drv->backend)
140 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700141
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800142 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700143 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700144
145 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700146 if (!drv->buffer_table)
147 goto free_lock;
148
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700149 drv->mappings = drv_array_init(sizeof(struct mapping));
150 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700151 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700152
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700153 drv->combos = drv_array_init(sizeof(struct combination));
154 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700155 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700156
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700157 if (drv->backend->init) {
158 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800159 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700160 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700161 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800162 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700163 }
164
165 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700166
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700167free_mappings:
168 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700169free_buffer_table:
170 drmHashDestroy(drv->buffer_table);
171free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800172 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700173free_driver:
174 free(drv);
175 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700176}
177
178void drv_destroy(struct driver *drv)
179{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800180 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700181
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700182 if (drv->backend->close)
183 drv->backend->close(drv);
184
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700185 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700186 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700187 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700188
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800189 pthread_mutex_unlock(&drv->driver_lock);
190 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700191
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700192 free(drv);
193}
194
195int drv_get_fd(struct driver *drv)
196{
197 return drv->fd;
198}
199
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800200const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700201{
202 return drv->backend->name;
203}
204
Gurchetan Singha1892b22017-09-28 16:40:52 -0700205struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700206{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208
Gurchetan Singha1892b22017-09-28 16:40:52 -0700209 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700210 return 0;
211
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800212 best = NULL;
213 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700214 for (i = 0; i < drv_array_size(drv->combos); i++) {
215 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700216 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800217 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800218 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219 }
220
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800221 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700222}
223
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700224struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000225 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700226{
227
228 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800229 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230
231 if (!bo)
232 return NULL;
233
234 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700235 bo->meta.width = width;
236 bo->meta.height = height;
237 bo->meta.format = format;
238 bo->meta.use_flags = use_flags;
239 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000240 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241
Gurchetan Singh298b7572019-09-19 09:55:18 -0700242 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700243 free(bo);
244 return NULL;
245 }
246
247 return bo;
248}
249
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800250struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700251 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252{
253 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700254 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700255 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000256 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700257
David Stevens26fe6822020-03-09 12:23:42 +0000258 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
259 use_flags &= ~BO_USE_TEST_ALLOC;
260
261 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700262
263 if (!bo)
264 return NULL;
265
David Stevens26fe6822020-03-09 12:23:42 +0000266 ret = -EINVAL;
267 if (drv->backend->bo_compute_metadata) {
268 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
269 0);
270 if (!is_test_alloc && ret == 0)
271 ret = drv->backend->bo_create_from_metadata(bo);
272 } else if (!is_test_alloc) {
273 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
274 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700275
276 if (ret) {
277 free(bo);
278 return NULL;
279 }
280
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800281 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700282
Gurchetan Singh298b7572019-09-19 09:55:18 -0700283 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700284 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700285 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700286
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700287 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700288 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700289
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800290 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700291
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700292 return bo;
293}
294
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800295struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
296 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700297{
298 int ret;
299 size_t plane;
300 struct bo *bo;
301
David Stevens26fe6822020-03-09 12:23:42 +0000302 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700303 errno = ENOENT;
304 return NULL;
305 }
306
David Stevens26fe6822020-03-09 12:23:42 +0000307 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700308
309 if (!bo)
310 return NULL;
311
David Stevens26fe6822020-03-09 12:23:42 +0000312 ret = -EINVAL;
313 if (drv->backend->bo_compute_metadata) {
314 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
315 modifiers, count);
316 if (ret == 0)
317 ret = drv->backend->bo_create_from_metadata(bo);
318 } else {
319 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
320 count);
321 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700322
323 if (ret) {
324 free(bo);
325 return NULL;
326 }
327
328 pthread_mutex_lock(&drv->driver_lock);
329
Gurchetan Singh298b7572019-09-19 09:55:18 -0700330 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700331 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700332 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700333
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700334 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700335 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700336
337 pthread_mutex_unlock(&drv->driver_lock);
338
339 return bo;
340}
341
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700342void drv_bo_destroy(struct bo *bo)
343{
Jason Macnak0907d822019-11-12 10:53:05 -0800344 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700345 size_t plane;
346 uintptr_t total = 0;
347 struct driver *drv = bo->drv;
348
David Stevens26fe6822020-03-09 12:23:42 +0000349 if (!bo->is_test_buffer) {
350 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700351
David Stevens26fe6822020-03-09 12:23:42 +0000352 for (plane = 0; plane < bo->meta.num_planes; plane++)
353 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700354
David Stevens26fe6822020-03-09 12:23:42 +0000355 for (plane = 0; plane < bo->meta.num_planes; plane++)
356 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700357
David Stevens26fe6822020-03-09 12:23:42 +0000358 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700359
David Stevens26fe6822020-03-09 12:23:42 +0000360 if (total == 0) {
361 ret = drv_mapping_destroy(bo);
362 assert(ret == 0);
363 bo->drv->backend->bo_destroy(bo);
364 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900365 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700366
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700367 free(bo);
368}
369
370struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
371{
372 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700373 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700374 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700375 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700376
David Stevens26fe6822020-03-09 12:23:42 +0000377 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700378
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700379 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700380 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700381
Gurchetan Singh71611d62017-01-03 16:49:56 -0800382 ret = drv->backend->bo_import(bo, data);
383 if (ret) {
384 free(bo);
385 return NULL;
386 }
387
Gurchetan Singh298b7572019-09-19 09:55:18 -0700388 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530389 pthread_mutex_lock(&bo->drv->driver_lock);
390 drv_increment_reference_count(bo->drv, bo, plane);
391 pthread_mutex_unlock(&bo->drv->driver_lock);
392 }
393
Gurchetan Singh52155b42021-01-27 17:55:17 -0800394 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700395 for (plane = 0; plane < bo->meta.num_planes; plane++) {
396 bo->meta.strides[plane] = data->strides[plane];
397 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700398
399 seek_end = lseek(data->fds[plane], 0, SEEK_END);
400 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700401 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700402 goto destroy_bo;
403 }
404
405 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700406 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
407 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700408 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700409 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700410
Gurchetan Singh298b7572019-09-19 09:55:18 -0700411 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700412 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700413 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800414 }
415
Gurchetan Singh298b7572019-09-19 09:55:18 -0700416 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700417 }
418
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700419 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700420
421destroy_bo:
422 drv_bo_destroy(bo);
423 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700424}
425
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800426void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
427 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700428{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700429 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700430 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700431 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700432
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800433 assert(rect->width >= 0);
434 assert(rect->height >= 0);
435 assert(rect->x + rect->width <= drv_bo_get_width(bo));
436 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700437 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900438 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700439 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700440
David Stevens26fe6822020-03-09 12:23:42 +0000441 if (bo->is_test_buffer) {
442 return MAP_FAILED;
443 }
444
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800445 mapping.rect = *rect;
446 mapping.refcount = 1;
447
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800448 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700449
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700450 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
451 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
452 if (prior->vma->handle != bo->handles[plane].u32 ||
453 prior->vma->map_flags != map_flags)
454 continue;
455
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800456 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
457 rect->width != prior->rect.width || rect->height != prior->rect.height)
458 continue;
459
460 prior->refcount++;
461 *map_data = prior;
462 goto exact_match;
463 }
464
465 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
466 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
467 if (prior->vma->handle != bo->handles[plane].u32 ||
468 prior->vma->map_flags != map_flags)
469 continue;
470
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700471 prior->vma->refcount++;
472 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473 goto success;
474 }
475
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700476 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700477 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800478 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700479 if (addr == MAP_FAILED) {
480 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700481 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800482 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700483 return MAP_FAILED;
484 }
485
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700486 mapping.vma->refcount = 1;
487 mapping.vma->addr = addr;
488 mapping.vma->handle = bo->handles[plane].u32;
489 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700490
491success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700492 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800493exact_match:
494 drv_bo_invalidate(bo, *map_data);
495 addr = (uint8_t *)((*map_data)->vma->addr);
496 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800497 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800498 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700499}
500
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700502{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700503 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700504 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700505
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800506 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700507
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800508 if (--mapping->refcount)
509 goto out;
510
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700511 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800512 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700513 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700514 }
515
516 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
517 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
518 drv_array_remove(bo->drv->mappings, i);
519 break;
520 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700521 }
522
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800523out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800524 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700525 return ret;
526}
527
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700528int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700529{
530 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700531
532 assert(mapping);
533 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800534 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700535 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700536
537 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700538 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700539
540 return ret;
541}
542
Jason Macnak1de7f662020-01-24 15:05:57 -0800543int drv_bo_flush(struct bo *bo, struct mapping *mapping)
544{
545 int ret = 0;
546
547 assert(mapping);
548 assert(mapping->vma);
549 assert(mapping->refcount > 0);
550 assert(mapping->vma->refcount > 0);
551
552 if (bo->drv->backend->bo_flush)
553 ret = bo->drv->backend->bo_flush(bo, mapping);
554
555 return ret;
556}
557
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700558int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700559{
560 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700561
562 assert(mapping);
563 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800564 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700565 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700566 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700567
568 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700569 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700570 else
571 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700572
573 return ret;
574}
575
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700576uint32_t drv_bo_get_width(struct bo *bo)
577{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700578 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700579}
580
581uint32_t drv_bo_get_height(struct bo *bo)
582{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700583 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700584}
585
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700586size_t drv_bo_get_num_planes(struct bo *bo)
587{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700588 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700589}
590
591union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
592{
593 return bo->handles[plane];
594}
595
596#ifndef DRM_RDWR
597#define DRM_RDWR O_RDWR
598#endif
599
600int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
601{
602
603 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700604 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700605
David Stevens26fe6822020-03-09 12:23:42 +0000606 if (bo->is_test_buffer) {
607 return -EINVAL;
608 }
609
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800610 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700611
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700612 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
613 if (ret)
614 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
615
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700616 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700617}
618
619uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
620{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700621 assert(plane < bo->meta.num_planes);
622 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700623}
624
625uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
626{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700627 assert(plane < bo->meta.num_planes);
628 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700629}
630
631uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
632{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700633 assert(plane < bo->meta.num_planes);
634 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700635}
636
Gurchetan Singh52155b42021-01-27 17:55:17 -0800637uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700638{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800639 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700640}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700641
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800642uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700643{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700644 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700645}
646
Jason Macnak1de7f662020-01-24 15:05:57 -0800647size_t drv_bo_get_total_size(struct bo *bo)
648{
649 return bo->meta.total_size;
650}
651
Gurchetan Singha1892b22017-09-28 16:40:52 -0700652uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700653{
654 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700655 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700656
657 return format;
658}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700659
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700660uint32_t drv_num_buffers_per_bo(struct bo *bo)
661{
662 uint32_t count = 0;
663 size_t plane, p;
664
David Stevens26fe6822020-03-09 12:23:42 +0000665 if (bo->is_test_buffer) {
666 return 0;
667 }
668
Gurchetan Singh298b7572019-09-19 09:55:18 -0700669 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700670 for (p = 0; p < plane; p++)
671 if (bo->handles[p].u32 == bo->handles[plane].u32)
672 break;
673 if (p == plane)
674 count++;
675 }
676
677 return count;
678}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700679
680void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
681{
682 char buf[50];
683 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
684
685 va_list args;
686 va_start(args, format);
687#ifdef __ANDROID__
688 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
689#else
690 fprintf(stderr, "%s ", buf);
691 vfprintf(stderr, format, args);
692#endif
693 va_end(args);
694}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700695
696int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
697 uint32_t offsets[DRV_MAX_PLANES])
698{
699 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
700 strides[plane] = bo->meta.strides[plane];
701 offsets[plane] = bo->meta.offsets[plane];
702 }
703
704 if (bo->drv->backend->resource_info)
705 return bo->drv->backend->resource_info(bo, strides, offsets);
706
707 return 0;
708}