blob: 233cce7d40e313810e047fb9ed90c1192415c700 [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;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070064static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070065{
66 drmVersionPtr drm_version;
67 unsigned int i;
68
69 drm_version = drmGetVersion(fd);
70
71 if (!drm_version)
72 return NULL;
73
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070074 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053075#ifdef DRV_AMDGPU
76 &backend_amdgpu,
77#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070078#ifdef DRV_EXYNOS
79 &backend_exynos,
80#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070081#ifdef DRV_I915
82 &backend_i915,
83#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070084#ifdef DRV_MEDIATEK
85 &backend_mediatek,
86#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053087#ifdef DRV_MSM
88 &backend_msm,
89#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070090#ifdef DRV_ROCKCHIP
91 &backend_rockchip,
92#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010093#ifdef DRV_VC4
94 &backend_vc4,
95#endif
Gurchetan Singh238001f2020-10-28 15:00:10 -070096 &backend_evdi, &backend_marvell, &backend_meson,
97 &backend_nouveau, &backend_komeda, &backend_radeon,
98 &backend_synaptics, &backend_virtio_gpu, &backend_udl,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070099 };
100
David Stevens26fe6822020-03-09 12:23:42 +0000101 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
102 const struct backend *b = backend_list[i];
103 // Exactly one of the main create functions must be defined.
104 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
105 // Either both or neither must be implemented.
106 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
107 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
108 assert((b->bo_create_with_modifiers == NULL) ||
109 (b->bo_create_from_metadata == NULL));
110
111 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700112 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000113 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700114 }
David Stevens26fe6822020-03-09 12:23:42 +0000115 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700116
117 drmFreeVersion(drm_version);
118 return NULL;
119}
120
121struct driver *drv_create(int fd)
122{
123 struct driver *drv;
124 int ret;
125
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800126 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700127
128 if (!drv)
129 return NULL;
130
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500131 char *minigbm_debug;
132 minigbm_debug = getenv("MINIGBM_DEBUG");
133 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
134
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700135 drv->fd = fd;
136 drv->backend = drv_get_backend(fd);
137
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700138 if (!drv->backend)
139 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700140
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800141 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700142 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700143
144 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700145 if (!drv->buffer_table)
146 goto free_lock;
147
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700148 drv->mappings = drv_array_init(sizeof(struct mapping));
149 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700150 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700151
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700152 drv->combos = drv_array_init(sizeof(struct combination));
153 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700154 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700155
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700156 if (drv->backend->init) {
157 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800158 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700159 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700160 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800161 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700162 }
163
164 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700165
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700166free_mappings:
167 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700168free_buffer_table:
169 drmHashDestroy(drv->buffer_table);
170free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800171 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700172free_driver:
173 free(drv);
174 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700175}
176
177void drv_destroy(struct driver *drv)
178{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800179 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 if (drv->backend->close)
182 drv->backend->close(drv);
183
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700184 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700185 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700186 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700187
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800188 pthread_mutex_unlock(&drv->driver_lock);
189 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700190
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700191 free(drv);
192}
193
194int drv_get_fd(struct driver *drv)
195{
196 return drv->fd;
197}
198
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800199const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200{
201 return drv->backend->name;
202}
203
Gurchetan Singha1892b22017-09-28 16:40:52 -0700204struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700205{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800206 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700207
Gurchetan Singha1892b22017-09-28 16:40:52 -0700208 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700209 return 0;
210
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800211 best = NULL;
212 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700213 for (i = 0; i < drv_array_size(drv->combos); i++) {
214 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700215 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800216 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800217 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700218 }
219
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800220 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221}
222
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700223struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000224 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700225{
226
227 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800228 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700229
230 if (!bo)
231 return NULL;
232
233 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700234 bo->meta.width = width;
235 bo->meta.height = height;
236 bo->meta.format = format;
237 bo->meta.use_flags = use_flags;
238 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000239 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700240
Gurchetan Singh298b7572019-09-19 09:55:18 -0700241 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700242 free(bo);
243 return NULL;
244 }
245
246 return bo;
247}
248
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800249struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700250 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700251{
252 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700253 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700254 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000255 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700256
David Stevens26fe6822020-03-09 12:23:42 +0000257 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
258 use_flags &= ~BO_USE_TEST_ALLOC;
259
260 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700261
262 if (!bo)
263 return NULL;
264
David Stevens26fe6822020-03-09 12:23:42 +0000265 ret = -EINVAL;
266 if (drv->backend->bo_compute_metadata) {
267 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
268 0);
269 if (!is_test_alloc && ret == 0)
270 ret = drv->backend->bo_create_from_metadata(bo);
271 } else if (!is_test_alloc) {
272 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
273 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700274
275 if (ret) {
276 free(bo);
277 return NULL;
278 }
279
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800280 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700281
Gurchetan Singh298b7572019-09-19 09:55:18 -0700282 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700283 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700284 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700285
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700286 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700287 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700288
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800289 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700290
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700291 return bo;
292}
293
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800294struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
295 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700296{
297 int ret;
298 size_t plane;
299 struct bo *bo;
300
David Stevens26fe6822020-03-09 12:23:42 +0000301 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700302 errno = ENOENT;
303 return NULL;
304 }
305
David Stevens26fe6822020-03-09 12:23:42 +0000306 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700307
308 if (!bo)
309 return NULL;
310
David Stevens26fe6822020-03-09 12:23:42 +0000311 ret = -EINVAL;
312 if (drv->backend->bo_compute_metadata) {
313 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
314 modifiers, count);
315 if (ret == 0)
316 ret = drv->backend->bo_create_from_metadata(bo);
317 } else {
318 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
319 count);
320 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700321
322 if (ret) {
323 free(bo);
324 return NULL;
325 }
326
327 pthread_mutex_lock(&drv->driver_lock);
328
Gurchetan Singh298b7572019-09-19 09:55:18 -0700329 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700330 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700331 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700332
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700333 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700334 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700335
336 pthread_mutex_unlock(&drv->driver_lock);
337
338 return bo;
339}
340
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700341void drv_bo_destroy(struct bo *bo)
342{
Jason Macnak0907d822019-11-12 10:53:05 -0800343 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700344 size_t plane;
345 uintptr_t total = 0;
346 struct driver *drv = bo->drv;
347
David Stevens26fe6822020-03-09 12:23:42 +0000348 if (!bo->is_test_buffer) {
349 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700350
David Stevens26fe6822020-03-09 12:23:42 +0000351 for (plane = 0; plane < bo->meta.num_planes; plane++)
352 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700353
David Stevens26fe6822020-03-09 12:23:42 +0000354 for (plane = 0; plane < bo->meta.num_planes; plane++)
355 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700356
David Stevens26fe6822020-03-09 12:23:42 +0000357 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700358
David Stevens26fe6822020-03-09 12:23:42 +0000359 if (total == 0) {
360 ret = drv_mapping_destroy(bo);
361 assert(ret == 0);
362 bo->drv->backend->bo_destroy(bo);
363 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900364 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700365
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700366 free(bo);
367}
368
369struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
370{
371 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700372 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700373 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700374 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700375
David Stevens26fe6822020-03-09 12:23:42 +0000376 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700377
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700378 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700379 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700380
Gurchetan Singh71611d62017-01-03 16:49:56 -0800381 ret = drv->backend->bo_import(bo, data);
382 if (ret) {
383 free(bo);
384 return NULL;
385 }
386
Gurchetan Singh298b7572019-09-19 09:55:18 -0700387 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530388 pthread_mutex_lock(&bo->drv->driver_lock);
389 drv_increment_reference_count(bo->drv, bo, plane);
390 pthread_mutex_unlock(&bo->drv->driver_lock);
391 }
392
Gurchetan Singh298b7572019-09-19 09:55:18 -0700393 for (plane = 0; plane < bo->meta.num_planes; plane++) {
394 bo->meta.strides[plane] = data->strides[plane];
395 bo->meta.offsets[plane] = data->offsets[plane];
396 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700397
398 seek_end = lseek(data->fds[plane], 0, SEEK_END);
399 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700400 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700401 goto destroy_bo;
402 }
403
404 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700405 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
406 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700407 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700408 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700409
Gurchetan Singh298b7572019-09-19 09:55:18 -0700410 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700411 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700412 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800413 }
414
Gurchetan Singh298b7572019-09-19 09:55:18 -0700415 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700416 }
417
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700418 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700419
420destroy_bo:
421 drv_bo_destroy(bo);
422 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700423}
424
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800425void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
426 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700428 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700429 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700430 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700431
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800432 assert(rect->width >= 0);
433 assert(rect->height >= 0);
434 assert(rect->x + rect->width <= drv_bo_get_width(bo));
435 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700436 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900437 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700438 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700439
David Stevens26fe6822020-03-09 12:23:42 +0000440 if (bo->is_test_buffer) {
441 return MAP_FAILED;
442 }
443
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800444 mapping.rect = *rect;
445 mapping.refcount = 1;
446
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800447 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700448
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700449 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
450 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
451 if (prior->vma->handle != bo->handles[plane].u32 ||
452 prior->vma->map_flags != map_flags)
453 continue;
454
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800455 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
456 rect->width != prior->rect.width || rect->height != prior->rect.height)
457 continue;
458
459 prior->refcount++;
460 *map_data = prior;
461 goto exact_match;
462 }
463
464 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
465 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
466 if (prior->vma->handle != bo->handles[plane].u32 ||
467 prior->vma->map_flags != map_flags)
468 continue;
469
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700470 prior->vma->refcount++;
471 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700472 goto success;
473 }
474
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700475 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700476 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800477 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700478 if (addr == MAP_FAILED) {
479 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700480 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800481 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700482 return MAP_FAILED;
483 }
484
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700485 mapping.vma->refcount = 1;
486 mapping.vma->addr = addr;
487 mapping.vma->handle = bo->handles[plane].u32;
488 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700489
490success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700491 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800492exact_match:
493 drv_bo_invalidate(bo, *map_data);
494 addr = (uint8_t *)((*map_data)->vma->addr);
495 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800496 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800497 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700498}
499
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700500int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700501{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700502 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700503 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700504
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800505 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700506
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800507 if (--mapping->refcount)
508 goto out;
509
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700510 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800511 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700512 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700513 }
514
515 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
516 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
517 drv_array_remove(bo->drv->mappings, i);
518 break;
519 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700520 }
521
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800522out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800523 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700524 return ret;
525}
526
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700527int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700528{
529 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700530
531 assert(mapping);
532 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800533 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700534 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700535
536 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700537 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700538
539 return ret;
540}
541
Jason Macnak1de7f662020-01-24 15:05:57 -0800542int drv_bo_flush(struct bo *bo, struct mapping *mapping)
543{
544 int ret = 0;
545
546 assert(mapping);
547 assert(mapping->vma);
548 assert(mapping->refcount > 0);
549 assert(mapping->vma->refcount > 0);
550
551 if (bo->drv->backend->bo_flush)
552 ret = bo->drv->backend->bo_flush(bo, mapping);
553
554 return ret;
555}
556
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700557int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700558{
559 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700560
561 assert(mapping);
562 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800563 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700564 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700565 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700566
567 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700568 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700569 else
570 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700571
572 return ret;
573}
574
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700575uint32_t drv_bo_get_width(struct bo *bo)
576{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700577 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700578}
579
580uint32_t drv_bo_get_height(struct bo *bo)
581{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700582 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700583}
584
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700585size_t drv_bo_get_num_planes(struct bo *bo)
586{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700587 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700588}
589
590union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
591{
592 return bo->handles[plane];
593}
594
595#ifndef DRM_RDWR
596#define DRM_RDWR O_RDWR
597#endif
598
599int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
600{
601
602 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700603 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700604
David Stevens26fe6822020-03-09 12:23:42 +0000605 if (bo->is_test_buffer) {
606 return -EINVAL;
607 }
608
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800609 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700610
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700611 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
612 if (ret)
613 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
614
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700615 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700616}
617
618uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
619{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700620 assert(plane < bo->meta.num_planes);
621 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700622}
623
624uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
625{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700626 assert(plane < bo->meta.num_planes);
627 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700628}
629
630uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
631{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700632 assert(plane < bo->meta.num_planes);
633 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700634}
635
636uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
637{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700638 assert(plane < bo->meta.num_planes);
639 return bo->meta.format_modifiers[plane];
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}