blob: 3f973c3cef2a5c1213bbc040ba179bac270e5c67 [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
Anders Delliene5bef532020-06-10 10:30:44 +010096 &backend_marvell, &backend_meson, &backend_nouveau, &backend_komeda,
97 &backend_radeon, &backend_synaptics, &backend_virtio_gpu,
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070098 };
99
David Stevens26fe6822020-03-09 12:23:42 +0000100 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
101 const struct backend *b = backend_list[i];
102 // Exactly one of the main create functions must be defined.
103 assert((b->bo_create != NULL) ^ (b->bo_create_from_metadata != NULL));
104 // Either both or neither must be implemented.
105 assert((b->bo_compute_metadata != NULL) == (b->bo_create_from_metadata != NULL));
106 // Both can't be defined, but it's okay for neither to be (i.e. only bo_create).
107 assert((b->bo_create_with_modifiers == NULL) ||
108 (b->bo_create_from_metadata == NULL));
109
110 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700111 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000112 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700113 }
David Stevens26fe6822020-03-09 12:23:42 +0000114 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700115
116 drmFreeVersion(drm_version);
117 return NULL;
118}
119
120struct driver *drv_create(int fd)
121{
122 struct driver *drv;
123 int ret;
124
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800125 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700126
127 if (!drv)
128 return NULL;
129
130 drv->fd = fd;
131 drv->backend = drv_get_backend(fd);
132
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700133 if (!drv->backend)
134 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700135
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800136 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700137 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700138
139 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700140 if (!drv->buffer_table)
141 goto free_lock;
142
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700143 drv->mappings = drv_array_init(sizeof(struct mapping));
144 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700145 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700146
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700147 drv->combos = drv_array_init(sizeof(struct combination));
148 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700149 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700150
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700151 if (drv->backend->init) {
152 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800153 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700154 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700155 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800156 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700157 }
158
159 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700160
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700161free_mappings:
162 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700163free_buffer_table:
164 drmHashDestroy(drv->buffer_table);
165free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800166 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700167free_driver:
168 free(drv);
169 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700170}
171
172void drv_destroy(struct driver *drv)
173{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800174 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700175
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700176 if (drv->backend->close)
177 drv->backend->close(drv);
178
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700179 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700180 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700181 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700182
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800183 pthread_mutex_unlock(&drv->driver_lock);
184 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700185
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700186 free(drv);
187}
188
189int drv_get_fd(struct driver *drv)
190{
191 return drv->fd;
192}
193
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800194const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700195{
196 return drv->backend->name;
197}
198
Gurchetan Singha1892b22017-09-28 16:40:52 -0700199struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700200{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800201 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700202
Gurchetan Singha1892b22017-09-28 16:40:52 -0700203 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700204 return 0;
205
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800206 best = NULL;
207 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700208 for (i = 0; i < drv_array_size(drv->combos); i++) {
209 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700210 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800211 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800212 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700213 }
214
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800215 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700216}
217
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700218struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000219 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700220{
221
222 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800223 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700224
225 if (!bo)
226 return NULL;
227
228 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700229 bo->meta.width = width;
230 bo->meta.height = height;
231 bo->meta.format = format;
232 bo->meta.use_flags = use_flags;
233 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000234 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700235
Gurchetan Singh298b7572019-09-19 09:55:18 -0700236 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700237 free(bo);
238 return NULL;
239 }
240
241 return bo;
242}
243
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800244struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700245 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246{
247 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700248 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700249 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000250 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700251
David Stevens26fe6822020-03-09 12:23:42 +0000252 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
253 use_flags &= ~BO_USE_TEST_ALLOC;
254
255 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700256
257 if (!bo)
258 return NULL;
259
David Stevens26fe6822020-03-09 12:23:42 +0000260 ret = -EINVAL;
261 if (drv->backend->bo_compute_metadata) {
262 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
263 0);
264 if (!is_test_alloc && ret == 0)
265 ret = drv->backend->bo_create_from_metadata(bo);
266 } else if (!is_test_alloc) {
267 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
268 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700269
270 if (ret) {
271 free(bo);
272 return NULL;
273 }
274
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800275 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700276
Gurchetan Singh298b7572019-09-19 09:55:18 -0700277 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700278 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700279 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700280
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700281 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700282 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700283
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800284 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700285
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700286 return bo;
287}
288
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800289struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
290 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700291{
292 int ret;
293 size_t plane;
294 struct bo *bo;
295
David Stevens26fe6822020-03-09 12:23:42 +0000296 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700297 errno = ENOENT;
298 return NULL;
299 }
300
David Stevens26fe6822020-03-09 12:23:42 +0000301 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700302
303 if (!bo)
304 return NULL;
305
David Stevens26fe6822020-03-09 12:23:42 +0000306 ret = -EINVAL;
307 if (drv->backend->bo_compute_metadata) {
308 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
309 modifiers, count);
310 if (ret == 0)
311 ret = drv->backend->bo_create_from_metadata(bo);
312 } else {
313 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
314 count);
315 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700316
317 if (ret) {
318 free(bo);
319 return NULL;
320 }
321
322 pthread_mutex_lock(&drv->driver_lock);
323
Gurchetan Singh298b7572019-09-19 09:55:18 -0700324 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700325 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700326 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700327
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700328 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700329 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700330
331 pthread_mutex_unlock(&drv->driver_lock);
332
333 return bo;
334}
335
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700336void drv_bo_destroy(struct bo *bo)
337{
Jason Macnak0907d822019-11-12 10:53:05 -0800338 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700339 size_t plane;
340 uintptr_t total = 0;
341 struct driver *drv = bo->drv;
342
David Stevens26fe6822020-03-09 12:23:42 +0000343 if (!bo->is_test_buffer) {
344 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700345
David Stevens26fe6822020-03-09 12:23:42 +0000346 for (plane = 0; plane < bo->meta.num_planes; plane++)
347 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700348
David Stevens26fe6822020-03-09 12:23:42 +0000349 for (plane = 0; plane < bo->meta.num_planes; plane++)
350 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700351
David Stevens26fe6822020-03-09 12:23:42 +0000352 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700353
David Stevens26fe6822020-03-09 12:23:42 +0000354 if (total == 0) {
355 ret = drv_mapping_destroy(bo);
356 assert(ret == 0);
357 bo->drv->backend->bo_destroy(bo);
358 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900359 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700360
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700361 free(bo);
362}
363
364struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
365{
366 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700367 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700368 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700369 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700370
David Stevens26fe6822020-03-09 12:23:42 +0000371 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700372
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700373 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700374 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700375
Gurchetan Singh71611d62017-01-03 16:49:56 -0800376 ret = drv->backend->bo_import(bo, data);
377 if (ret) {
378 free(bo);
379 return NULL;
380 }
381
Gurchetan Singh298b7572019-09-19 09:55:18 -0700382 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530383 pthread_mutex_lock(&bo->drv->driver_lock);
384 drv_increment_reference_count(bo->drv, bo, plane);
385 pthread_mutex_unlock(&bo->drv->driver_lock);
386 }
387
Gurchetan Singh298b7572019-09-19 09:55:18 -0700388 for (plane = 0; plane < bo->meta.num_planes; plane++) {
389 bo->meta.strides[plane] = data->strides[plane];
390 bo->meta.offsets[plane] = data->offsets[plane];
391 bo->meta.format_modifiers[plane] = data->format_modifiers[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700392
393 seek_end = lseek(data->fds[plane], 0, SEEK_END);
394 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700395 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700396 goto destroy_bo;
397 }
398
399 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700400 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
401 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700402 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700403 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700404
Gurchetan Singh298b7572019-09-19 09:55:18 -0700405 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700406 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700407 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800408 }
409
Gurchetan Singh298b7572019-09-19 09:55:18 -0700410 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700411 }
412
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700413 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700414
415destroy_bo:
416 drv_bo_destroy(bo);
417 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700418}
419
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800420void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
421 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700422{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700423 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700424 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700425 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700426
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800427 assert(rect->width >= 0);
428 assert(rect->height >= 0);
429 assert(rect->x + rect->width <= drv_bo_get_width(bo));
430 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700431 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900432 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700433 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700434
David Stevens26fe6822020-03-09 12:23:42 +0000435 if (bo->is_test_buffer) {
436 return MAP_FAILED;
437 }
438
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800439 mapping.rect = *rect;
440 mapping.refcount = 1;
441
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800442 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700443
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700444 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
445 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
446 if (prior->vma->handle != bo->handles[plane].u32 ||
447 prior->vma->map_flags != map_flags)
448 continue;
449
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800450 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
451 rect->width != prior->rect.width || rect->height != prior->rect.height)
452 continue;
453
454 prior->refcount++;
455 *map_data = prior;
456 goto exact_match;
457 }
458
459 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
460 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
461 if (prior->vma->handle != bo->handles[plane].u32 ||
462 prior->vma->map_flags != map_flags)
463 continue;
464
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700465 prior->vma->refcount++;
466 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700467 goto success;
468 }
469
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700470 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700471 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800472 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700473 if (addr == MAP_FAILED) {
474 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700475 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800476 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700477 return MAP_FAILED;
478 }
479
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700480 mapping.vma->refcount = 1;
481 mapping.vma->addr = addr;
482 mapping.vma->handle = bo->handles[plane].u32;
483 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700484
485success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700486 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800487exact_match:
488 drv_bo_invalidate(bo, *map_data);
489 addr = (uint8_t *)((*map_data)->vma->addr);
490 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800491 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800492 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700493}
494
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700495int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700496{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700497 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700498 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700499
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800500 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700501
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800502 if (--mapping->refcount)
503 goto out;
504
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700505 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800506 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700507 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700508 }
509
510 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
511 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
512 drv_array_remove(bo->drv->mappings, i);
513 break;
514 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700515 }
516
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800517out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800518 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700519 return ret;
520}
521
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700522int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700523{
524 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700525
526 assert(mapping);
527 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800528 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700529 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700530
531 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700532 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700533
534 return ret;
535}
536
Jason Macnak1de7f662020-01-24 15:05:57 -0800537int drv_bo_flush(struct bo *bo, struct mapping *mapping)
538{
539 int ret = 0;
540
541 assert(mapping);
542 assert(mapping->vma);
543 assert(mapping->refcount > 0);
544 assert(mapping->vma->refcount > 0);
545
546 if (bo->drv->backend->bo_flush)
547 ret = bo->drv->backend->bo_flush(bo, mapping);
548
549 return ret;
550}
551
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700552int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700553{
554 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700555
556 assert(mapping);
557 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800558 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700559 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700560 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700561
562 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700563 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700564 else
565 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700566
567 return ret;
568}
569
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700570uint32_t drv_bo_get_width(struct bo *bo)
571{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700572 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700573}
574
575uint32_t drv_bo_get_height(struct bo *bo)
576{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700577 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700578}
579
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700580size_t drv_bo_get_num_planes(struct bo *bo)
581{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700582 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700583}
584
585union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
586{
587 return bo->handles[plane];
588}
589
590#ifndef DRM_RDWR
591#define DRM_RDWR O_RDWR
592#endif
593
594int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
595{
596
597 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700598 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700599
David Stevens26fe6822020-03-09 12:23:42 +0000600 if (bo->is_test_buffer) {
601 return -EINVAL;
602 }
603
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800604 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700605
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700606 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
607 if (ret)
608 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
609
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700610 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700611}
612
613uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
614{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700615 assert(plane < bo->meta.num_planes);
616 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700617}
618
619uint32_t drv_bo_get_plane_size(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.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700623}
624
625uint32_t drv_bo_get_plane_stride(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.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700629}
630
631uint64_t drv_bo_get_plane_format_modifier(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.format_modifiers[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700635}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700636
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800637uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700638{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700639 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700640}
641
Jason Macnak1de7f662020-01-24 15:05:57 -0800642size_t drv_bo_get_total_size(struct bo *bo)
643{
644 return bo->meta.total_size;
645}
646
Gurchetan Singha1892b22017-09-28 16:40:52 -0700647uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700648{
649 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700650 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700651
652 return format;
653}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700654
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700655uint32_t drv_num_buffers_per_bo(struct bo *bo)
656{
657 uint32_t count = 0;
658 size_t plane, p;
659
David Stevens26fe6822020-03-09 12:23:42 +0000660 if (bo->is_test_buffer) {
661 return 0;
662 }
663
Gurchetan Singh298b7572019-09-19 09:55:18 -0700664 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700665 for (p = 0; p < plane; p++)
666 if (bo->handles[p].u32 == bo->handles[plane].u32)
667 break;
668 if (p == plane)
669 count++;
670 }
671
672 return count;
673}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700674
675void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
676{
677 char buf[50];
678 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
679
680 va_list args;
681 va_start(args, format);
682#ifdef __ANDROID__
683 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
684#else
685 fprintf(stderr, "%s ", buf);
686 vfprintf(stderr, format, args);
687#endif
688 va_end(args);
689}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700690
691int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
692 uint32_t offsets[DRV_MAX_PLANES])
693{
694 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
695 strides[plane] = bo->meta.strides[plane];
696 offsets[plane] = bo->meta.offsets[plane];
697 }
698
699 if (bo->drv->backend->resource_info)
700 return bo->drv->backend->resource_info(bo, strides, offsets);
701
702 return 0;
703}