blob: 10e8f8d65fc731753d06daa9ba7f7761a6dae541 [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
131 drv->fd = fd;
132 drv->backend = drv_get_backend(fd);
133
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700134 if (!drv->backend)
135 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700136
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800137 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700138 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700139
140 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700141 if (!drv->buffer_table)
142 goto free_lock;
143
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700144 drv->mappings = drv_array_init(sizeof(struct mapping));
145 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700146 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700147
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700148 drv->combos = drv_array_init(sizeof(struct combination));
149 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700150 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700151
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152 if (drv->backend->init) {
153 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800154 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700155 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700156 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800157 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700158 }
159
160 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700161
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700162free_mappings:
163 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700164free_buffer_table:
165 drmHashDestroy(drv->buffer_table);
166free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800167 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700168free_driver:
169 free(drv);
170 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700171}
172
173void drv_destroy(struct driver *drv)
174{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800175 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700176
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700177 if (drv->backend->close)
178 drv->backend->close(drv);
179
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700180 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700181 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700182 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700183
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800184 pthread_mutex_unlock(&drv->driver_lock);
185 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700186
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700187 free(drv);
188}
189
190int drv_get_fd(struct driver *drv)
191{
192 return drv->fd;
193}
194
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800195const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700196{
197 return drv->backend->name;
198}
199
Gurchetan Singha1892b22017-09-28 16:40:52 -0700200struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700201{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800202 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700203
Gurchetan Singha1892b22017-09-28 16:40:52 -0700204 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700205 return 0;
206
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 best = NULL;
208 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700209 for (i = 0; i < drv_array_size(drv->combos); i++) {
210 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700211 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800212 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800213 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700214 }
215
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800216 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700217}
218
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700219struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000220 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700221{
222
223 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800224 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700225
226 if (!bo)
227 return NULL;
228
229 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700230 bo->meta.width = width;
231 bo->meta.height = height;
232 bo->meta.format = format;
233 bo->meta.use_flags = use_flags;
234 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000235 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700236
Gurchetan Singh298b7572019-09-19 09:55:18 -0700237 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700238 free(bo);
239 return NULL;
240 }
241
242 return bo;
243}
244
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800245struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700246 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700247{
248 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700249 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700250 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000251 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700252
David Stevens26fe6822020-03-09 12:23:42 +0000253 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
254 use_flags &= ~BO_USE_TEST_ALLOC;
255
256 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700257
258 if (!bo)
259 return NULL;
260
David Stevens26fe6822020-03-09 12:23:42 +0000261 ret = -EINVAL;
262 if (drv->backend->bo_compute_metadata) {
263 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
264 0);
265 if (!is_test_alloc && ret == 0)
266 ret = drv->backend->bo_create_from_metadata(bo);
267 } else if (!is_test_alloc) {
268 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
269 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700270
271 if (ret) {
272 free(bo);
273 return NULL;
274 }
275
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800276 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700277
Gurchetan Singh298b7572019-09-19 09:55:18 -0700278 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700279 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700280 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700281
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700282 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700283 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700284
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800285 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700286
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700287 return bo;
288}
289
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800290struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
291 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700292{
293 int ret;
294 size_t plane;
295 struct bo *bo;
296
David Stevens26fe6822020-03-09 12:23:42 +0000297 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700298 errno = ENOENT;
299 return NULL;
300 }
301
David Stevens26fe6822020-03-09 12:23:42 +0000302 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700303
304 if (!bo)
305 return NULL;
306
David Stevens26fe6822020-03-09 12:23:42 +0000307 ret = -EINVAL;
308 if (drv->backend->bo_compute_metadata) {
309 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
310 modifiers, count);
311 if (ret == 0)
312 ret = drv->backend->bo_create_from_metadata(bo);
313 } else {
314 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
315 count);
316 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700317
318 if (ret) {
319 free(bo);
320 return NULL;
321 }
322
323 pthread_mutex_lock(&drv->driver_lock);
324
Gurchetan Singh298b7572019-09-19 09:55:18 -0700325 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700326 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700327 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700328
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700329 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700330 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700331
332 pthread_mutex_unlock(&drv->driver_lock);
333
334 return bo;
335}
336
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700337void drv_bo_destroy(struct bo *bo)
338{
Jason Macnak0907d822019-11-12 10:53:05 -0800339 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700340 size_t plane;
341 uintptr_t total = 0;
342 struct driver *drv = bo->drv;
343
David Stevens26fe6822020-03-09 12:23:42 +0000344 if (!bo->is_test_buffer) {
345 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700346
David Stevens26fe6822020-03-09 12:23:42 +0000347 for (plane = 0; plane < bo->meta.num_planes; plane++)
348 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700349
David Stevens26fe6822020-03-09 12:23:42 +0000350 for (plane = 0; plane < bo->meta.num_planes; plane++)
351 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700352
David Stevens26fe6822020-03-09 12:23:42 +0000353 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700354
David Stevens26fe6822020-03-09 12:23:42 +0000355 if (total == 0) {
356 ret = drv_mapping_destroy(bo);
357 assert(ret == 0);
358 bo->drv->backend->bo_destroy(bo);
359 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900360 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700361
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700362 free(bo);
363}
364
365struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
366{
367 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700368 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700369 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700370 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700371
David Stevens26fe6822020-03-09 12:23:42 +0000372 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700373
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700374 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700375 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700376
Gurchetan Singh71611d62017-01-03 16:49:56 -0800377 ret = drv->backend->bo_import(bo, data);
378 if (ret) {
379 free(bo);
380 return NULL;
381 }
382
Gurchetan Singh298b7572019-09-19 09:55:18 -0700383 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530384 pthread_mutex_lock(&bo->drv->driver_lock);
385 drv_increment_reference_count(bo->drv, bo, plane);
386 pthread_mutex_unlock(&bo->drv->driver_lock);
387 }
388
Gurchetan Singh298b7572019-09-19 09:55:18 -0700389 for (plane = 0; plane < bo->meta.num_planes; plane++) {
390 bo->meta.strides[plane] = data->strides[plane];
391 bo->meta.offsets[plane] = data->offsets[plane];
392 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700393
394 seek_end = lseek(data->fds[plane], 0, SEEK_END);
395 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700396 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700397 goto destroy_bo;
398 }
399
400 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700401 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
402 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700403 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700404 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700405
Gurchetan Singh298b7572019-09-19 09:55:18 -0700406 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700407 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700408 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800409 }
410
Gurchetan Singh298b7572019-09-19 09:55:18 -0700411 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700412 }
413
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700414 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700415
416destroy_bo:
417 drv_bo_destroy(bo);
418 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700419}
420
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800421void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
422 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700423{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700424 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700425 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700426 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700427
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800428 assert(rect->width >= 0);
429 assert(rect->height >= 0);
430 assert(rect->x + rect->width <= drv_bo_get_width(bo));
431 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700432 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900433 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700434 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700435
David Stevens26fe6822020-03-09 12:23:42 +0000436 if (bo->is_test_buffer) {
437 return MAP_FAILED;
438 }
439
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800440 mapping.rect = *rect;
441 mapping.refcount = 1;
442
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800443 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700444
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700445 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
446 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
447 if (prior->vma->handle != bo->handles[plane].u32 ||
448 prior->vma->map_flags != map_flags)
449 continue;
450
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800451 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
452 rect->width != prior->rect.width || rect->height != prior->rect.height)
453 continue;
454
455 prior->refcount++;
456 *map_data = prior;
457 goto exact_match;
458 }
459
460 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
461 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
462 if (prior->vma->handle != bo->handles[plane].u32 ||
463 prior->vma->map_flags != map_flags)
464 continue;
465
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700466 prior->vma->refcount++;
467 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700468 goto success;
469 }
470
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700471 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700472 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800473 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700474 if (addr == MAP_FAILED) {
475 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700476 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800477 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700478 return MAP_FAILED;
479 }
480
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700481 mapping.vma->refcount = 1;
482 mapping.vma->addr = addr;
483 mapping.vma->handle = bo->handles[plane].u32;
484 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700485
486success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700487 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800488exact_match:
489 drv_bo_invalidate(bo, *map_data);
490 addr = (uint8_t *)((*map_data)->vma->addr);
491 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800492 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800493 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700494}
495
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700496int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700497{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700498 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700499 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700500
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800501 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700502
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800503 if (--mapping->refcount)
504 goto out;
505
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700506 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800507 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700508 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700509 }
510
511 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
512 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
513 drv_array_remove(bo->drv->mappings, i);
514 break;
515 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700516 }
517
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800518out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800519 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700520 return ret;
521}
522
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700523int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700524{
525 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700526
527 assert(mapping);
528 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800529 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700530 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700531
532 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700533 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700534
535 return ret;
536}
537
Jason Macnak1de7f662020-01-24 15:05:57 -0800538int drv_bo_flush(struct bo *bo, struct mapping *mapping)
539{
540 int ret = 0;
541
542 assert(mapping);
543 assert(mapping->vma);
544 assert(mapping->refcount > 0);
545 assert(mapping->vma->refcount > 0);
546
547 if (bo->drv->backend->bo_flush)
548 ret = bo->drv->backend->bo_flush(bo, mapping);
549
550 return ret;
551}
552
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700553int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700554{
555 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700556
557 assert(mapping);
558 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800559 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700560 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700561 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700562
563 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700564 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700565 else
566 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700567
568 return ret;
569}
570
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700571uint32_t drv_bo_get_width(struct bo *bo)
572{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700573 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700574}
575
576uint32_t drv_bo_get_height(struct bo *bo)
577{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700578 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700579}
580
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700581size_t drv_bo_get_num_planes(struct bo *bo)
582{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700583 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700584}
585
586union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
587{
588 return bo->handles[plane];
589}
590
591#ifndef DRM_RDWR
592#define DRM_RDWR O_RDWR
593#endif
594
595int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
596{
597
598 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700599 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700600
David Stevens26fe6822020-03-09 12:23:42 +0000601 if (bo->is_test_buffer) {
602 return -EINVAL;
603 }
604
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800605 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700606
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700607 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
608 if (ret)
609 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
610
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700611 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700612}
613
614uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
615{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700616 assert(plane < bo->meta.num_planes);
617 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700618}
619
620uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
621{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700622 assert(plane < bo->meta.num_planes);
623 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700624}
625
626uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
627{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700628 assert(plane < bo->meta.num_planes);
629 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700630}
631
632uint64_t drv_bo_get_plane_format_modifier(struct bo *bo, size_t plane)
633{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700634 assert(plane < bo->meta.num_planes);
635 return bo->meta.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700636}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700637
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800638uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700639{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700640 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700641}
642
Jason Macnak1de7f662020-01-24 15:05:57 -0800643size_t drv_bo_get_total_size(struct bo *bo)
644{
645 return bo->meta.total_size;
646}
647
Gurchetan Singha1892b22017-09-28 16:40:52 -0700648uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700649{
650 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700651 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700652
653 return format;
654}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700655
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700656uint32_t drv_num_buffers_per_bo(struct bo *bo)
657{
658 uint32_t count = 0;
659 size_t plane, p;
660
David Stevens26fe6822020-03-09 12:23:42 +0000661 if (bo->is_test_buffer) {
662 return 0;
663 }
664
Gurchetan Singh298b7572019-09-19 09:55:18 -0700665 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700666 for (p = 0; p < plane; p++)
667 if (bo->handles[p].u32 == bo->handles[plane].u32)
668 break;
669 if (p == plane)
670 count++;
671 }
672
673 return count;
674}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700675
676void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
677{
678 char buf[50];
679 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
680
681 va_list args;
682 va_start(args, format);
683#ifdef __ANDROID__
684 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
685#else
686 fprintf(stderr, "%s ", buf);
687 vfprintf(stderr, format, args);
688#endif
689 va_end(args);
690}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700691
692int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
693 uint32_t offsets[DRV_MAX_PLANES])
694{
695 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
696 strides[plane] = bo->meta.strides[plane];
697 offsets[plane] = bo->meta.offsets[plane];
698 }
699
700 if (bo->drv->backend->resource_info)
701 return bo->drv->backend->resource_info(bo, strides, offsets);
702
703 return 0;
704}