blob: 2f8d5478a945faf4bc47d09d86f6d5ad5e438f81 [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
Niklas Schulze878fed42017-02-08 15:29:21 +010046#ifdef DRV_VC4
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070047extern const struct backend backend_vc4;
Niklas Schulze878fed42017-02-08 15:29:21 +010048#endif
Anders Delliene5bef532020-06-10 10:30:44 +010049
50// Dumb / generic drivers
51extern const struct backend backend_evdi;
52extern const struct backend backend_marvell;
53extern const struct backend backend_meson;
54extern const struct backend backend_nouveau;
55extern const struct backend backend_komeda;
56extern const struct backend backend_radeon;
57extern const struct backend backend_synaptics;
Gurchetan Singh73c141e2021-01-21 14:51:19 -080058extern const struct backend backend_virtgpu;
Anders Delliene5bef532020-06-10 10:30:44 +010059extern const struct backend backend_udl;
François-Denis Gonthiercea0b842020-05-22 18:02:24 -040060extern const struct backend backend_vkms;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070061
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070062static const struct backend *drv_get_backend(int fd)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070063{
64 drmVersionPtr drm_version;
65 unsigned int i;
66
67 drm_version = drmGetVersion(fd);
68
69 if (!drm_version)
70 return NULL;
71
Gurchetan Singh3e9d3832017-10-31 10:36:25 -070072 const struct backend *backend_list[] = {
Akshu Agrawal0337d9b2016-07-28 15:35:45 +053073#ifdef DRV_AMDGPU
74 &backend_amdgpu,
75#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070076#ifdef DRV_EXYNOS
77 &backend_exynos,
78#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070079#ifdef DRV_I915
80 &backend_i915,
81#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070082#ifdef DRV_MEDIATEK
83 &backend_mediatek,
84#endif
Rajesh Yadav7f79cb52018-01-22 18:29:06 +053085#ifdef DRV_MSM
86 &backend_msm,
87#endif
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070088#ifdef DRV_ROCKCHIP
89 &backend_rockchip,
90#endif
Niklas Schulze878fed42017-02-08 15:29:21 +010091#ifdef DRV_VC4
92 &backend_vc4,
93#endif
Gurchetan Singh73c141e2021-01-21 14:51:19 -080094 &backend_evdi, &backend_marvell, &backend_meson, &backend_nouveau,
95 &backend_komeda, &backend_radeon, &backend_synaptics, &backend_virtgpu,
96 &backend_udl, &backend_virtgpu, &backend_vkms
Gurchetan Singh46faf6b2016-08-05 14:40:07 -070097 };
98
David Stevens26fe6822020-03-09 12:23:42 +000099 for (i = 0; i < ARRAY_SIZE(backend_list); i++) {
100 const struct backend *b = backend_list[i];
David Stevens26fe6822020-03-09 12:23:42 +0000101 if (!strcmp(drm_version->name, b->name)) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700102 drmFreeVersion(drm_version);
David Stevens26fe6822020-03-09 12:23:42 +0000103 return b;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700104 }
David Stevens26fe6822020-03-09 12:23:42 +0000105 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700106
107 drmFreeVersion(drm_version);
108 return NULL;
109}
110
111struct driver *drv_create(int fd)
112{
113 struct driver *drv;
114 int ret;
115
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800116 drv = (struct driver *)calloc(1, sizeof(*drv));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700117
118 if (!drv)
119 return NULL;
120
Pilar Molina Lopez28cf2f12020-11-12 18:19:42 -0500121 char *minigbm_debug;
122 minigbm_debug = getenv("MINIGBM_DEBUG");
123 drv->compression = (minigbm_debug == NULL) || (strcmp(minigbm_debug, "nocompression") != 0);
124
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700125 drv->fd = fd;
126 drv->backend = drv_get_backend(fd);
127
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700128 if (!drv->backend)
129 goto free_driver;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700130
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800131 if (pthread_mutex_init(&drv->driver_lock, NULL))
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700132 goto free_driver;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700133
134 drv->buffer_table = drmHashCreate();
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700135 if (!drv->buffer_table)
136 goto free_lock;
137
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700138 drv->mappings = drv_array_init(sizeof(struct mapping));
139 if (!drv->mappings)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700140 goto free_buffer_table;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700141
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700142 drv->combos = drv_array_init(sizeof(struct combination));
143 if (!drv->combos)
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700144 goto free_mappings;
Gurchetan Singh179687e2016-10-28 10:07:35 -0700145
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700146 if (drv->backend->init) {
147 ret = drv->backend->init(drv);
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800148 if (ret) {
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700149 drv_array_destroy(drv->combos);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700150 goto free_mappings;
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800151 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700152 }
153
154 return drv;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700155
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700156free_mappings:
157 drv_array_destroy(drv->mappings);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700158free_buffer_table:
159 drmHashDestroy(drv->buffer_table);
160free_lock:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800161 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700162free_driver:
163 free(drv);
164 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700165}
166
167void drv_destroy(struct driver *drv)
168{
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800169 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700170
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700171 if (drv->backend->close)
172 drv->backend->close(drv);
173
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700174 drmHashDestroy(drv->buffer_table);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700175 drv_array_destroy(drv->mappings);
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700176 drv_array_destroy(drv->combos);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700177
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800178 pthread_mutex_unlock(&drv->driver_lock);
179 pthread_mutex_destroy(&drv->driver_lock);
Gurchetan Singh179687e2016-10-28 10:07:35 -0700180
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700181 free(drv);
182}
183
184int drv_get_fd(struct driver *drv)
185{
186 return drv->fd;
187}
188
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800189const char *drv_get_name(struct driver *drv)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700190{
191 return drv->backend->name;
192}
193
Gurchetan Singha1892b22017-09-28 16:40:52 -0700194struct combination *drv_get_combination(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700195{
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800196 struct combination *curr, *best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700197
Gurchetan Singha1892b22017-09-28 16:40:52 -0700198 if (format == DRM_FORMAT_NONE || use_flags == BO_USE_NONE)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700199 return 0;
200
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800201 best = NULL;
202 uint32_t i;
Gurchetan Singhbc9a87d2017-11-03 17:17:35 -0700203 for (i = 0; i < drv_array_size(drv->combos); i++) {
204 curr = drv_array_at_idx(drv->combos, i);
Gurchetan Singha1892b22017-09-28 16:40:52 -0700205 if ((format == curr->format) && use_flags == (curr->use_flags & use_flags))
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800206 if (!best || best->metadata.priority < curr->metadata.priority)
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800207 best = curr;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700208 }
209
Gurchetan Singh6b41fb52017-03-01 20:14:39 -0800210 return best;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700211}
212
Gurchetan Singh18578ed2017-08-03 18:23:27 -0700213struct bo *drv_bo_new(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
David Stevens26fe6822020-03-09 12:23:42 +0000214 uint64_t use_flags, bool is_test_buffer)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700215{
216
217 struct bo *bo;
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800218 bo = (struct bo *)calloc(1, sizeof(*bo));
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700219
220 if (!bo)
221 return NULL;
222
223 bo->drv = drv;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700224 bo->meta.width = width;
225 bo->meta.height = height;
226 bo->meta.format = format;
227 bo->meta.use_flags = use_flags;
228 bo->meta.num_planes = drv_num_planes_from_format(format);
David Stevens26fe6822020-03-09 12:23:42 +0000229 bo->is_test_buffer = is_test_buffer;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700230
Gurchetan Singh298b7572019-09-19 09:55:18 -0700231 if (!bo->meta.num_planes) {
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700232 free(bo);
233 return NULL;
234 }
235
236 return bo;
237}
238
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800239struct bo *drv_bo_create(struct driver *drv, uint32_t width, uint32_t height, uint32_t format,
Gurchetan Singha1892b22017-09-28 16:40:52 -0700240 uint64_t use_flags)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700241{
242 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700243 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700244 struct bo *bo;
David Stevens26fe6822020-03-09 12:23:42 +0000245 bool is_test_alloc;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700246
David Stevens26fe6822020-03-09 12:23:42 +0000247 is_test_alloc = use_flags & BO_USE_TEST_ALLOC;
248 use_flags &= ~BO_USE_TEST_ALLOC;
249
250 bo = drv_bo_new(drv, width, height, format, use_flags, is_test_alloc);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700251
252 if (!bo)
253 return NULL;
254
David Stevens26fe6822020-03-09 12:23:42 +0000255 ret = -EINVAL;
256 if (drv->backend->bo_compute_metadata) {
257 ret = drv->backend->bo_compute_metadata(bo, width, height, format, use_flags, NULL,
258 0);
259 if (!is_test_alloc && ret == 0)
260 ret = drv->backend->bo_create_from_metadata(bo);
261 } else if (!is_test_alloc) {
262 ret = drv->backend->bo_create(bo, width, height, format, use_flags);
263 }
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700264
265 if (ret) {
266 free(bo);
267 return NULL;
268 }
269
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800270 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700271
Gurchetan Singh298b7572019-09-19 09:55:18 -0700272 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700273 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700274 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700275
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700276 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700277 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700278
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800279 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700280
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700281 return bo;
282}
283
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800284struct bo *drv_bo_create_with_modifiers(struct driver *drv, uint32_t width, uint32_t height,
285 uint32_t format, const uint64_t *modifiers, uint32_t count)
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700286{
287 int ret;
288 size_t plane;
289 struct bo *bo;
290
David Stevens26fe6822020-03-09 12:23:42 +0000291 if (!drv->backend->bo_create_with_modifiers && !drv->backend->bo_compute_metadata) {
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700292 errno = ENOENT;
293 return NULL;
294 }
295
David Stevens26fe6822020-03-09 12:23:42 +0000296 bo = drv_bo_new(drv, width, height, format, BO_USE_NONE, false);
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700297
298 if (!bo)
299 return NULL;
300
David Stevens26fe6822020-03-09 12:23:42 +0000301 ret = -EINVAL;
302 if (drv->backend->bo_compute_metadata) {
303 ret = drv->backend->bo_compute_metadata(bo, width, height, format, BO_USE_NONE,
304 modifiers, count);
305 if (ret == 0)
306 ret = drv->backend->bo_create_from_metadata(bo);
307 } else {
308 ret = drv->backend->bo_create_with_modifiers(bo, width, height, format, modifiers,
309 count);
310 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700311
312 if (ret) {
313 free(bo);
314 return NULL;
315 }
316
317 pthread_mutex_lock(&drv->driver_lock);
318
Gurchetan Singh298b7572019-09-19 09:55:18 -0700319 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700320 if (plane > 0)
Gurchetan Singh298b7572019-09-19 09:55:18 -0700321 assert(bo->meta.offsets[plane] >= bo->meta.offsets[plane - 1]);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700322
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700323 drv_increment_reference_count(drv, bo, plane);
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700324 }
Kristian H. Kristensenb1efbd82016-09-06 11:43:26 -0700325
326 pthread_mutex_unlock(&drv->driver_lock);
327
328 return bo;
329}
330
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700331void drv_bo_destroy(struct bo *bo)
332{
Jason Macnak0907d822019-11-12 10:53:05 -0800333 int ret;
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700334 size_t plane;
335 uintptr_t total = 0;
336 struct driver *drv = bo->drv;
337
David Stevens26fe6822020-03-09 12:23:42 +0000338 if (!bo->is_test_buffer) {
339 pthread_mutex_lock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700340
David Stevens26fe6822020-03-09 12:23:42 +0000341 for (plane = 0; plane < bo->meta.num_planes; plane++)
342 drv_decrement_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700343
David Stevens26fe6822020-03-09 12:23:42 +0000344 for (plane = 0; plane < bo->meta.num_planes; plane++)
345 total += drv_get_reference_count(drv, bo, plane);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700346
David Stevens26fe6822020-03-09 12:23:42 +0000347 pthread_mutex_unlock(&drv->driver_lock);
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700348
David Stevens26fe6822020-03-09 12:23:42 +0000349 if (total == 0) {
350 ret = drv_mapping_destroy(bo);
351 assert(ret == 0);
352 bo->drv->backend->bo_destroy(bo);
353 }
Tomasz Figa27a7e6a2017-08-08 17:59:41 +0900354 }
Gurchetan Singh1647fbe2016-08-03 17:14:55 -0700355
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700356 free(bo);
357}
358
359struct bo *drv_bo_import(struct driver *drv, struct drv_import_fd_data *data)
360{
361 int ret;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700362 size_t plane;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700363 struct bo *bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700364 off_t seek_end;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700365
David Stevens26fe6822020-03-09 12:23:42 +0000366 bo = drv_bo_new(drv, data->width, data->height, data->format, data->use_flags, false);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700367
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700368 if (!bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700369 return NULL;
Gurchetan Singhb72badb2016-08-19 16:26:46 -0700370
Gurchetan Singh71611d62017-01-03 16:49:56 -0800371 ret = drv->backend->bo_import(bo, data);
372 if (ret) {
373 free(bo);
374 return NULL;
375 }
376
Gurchetan Singh298b7572019-09-19 09:55:18 -0700377 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Satyajit Sahua047d412018-07-12 12:29:39 +0530378 pthread_mutex_lock(&bo->drv->driver_lock);
379 drv_increment_reference_count(bo->drv, bo, plane);
380 pthread_mutex_unlock(&bo->drv->driver_lock);
381 }
382
Gurchetan Singh52155b42021-01-27 17:55:17 -0800383 bo->meta.format_modifier = data->format_modifier;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700384 for (plane = 0; plane < bo->meta.num_planes; plane++) {
385 bo->meta.strides[plane] = data->strides[plane];
386 bo->meta.offsets[plane] = data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700387
388 seek_end = lseek(data->fds[plane], 0, SEEK_END);
389 if (seek_end == (off_t)(-1)) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700390 drv_log("lseek() failed with %s\n", strerror(errno));
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700391 goto destroy_bo;
392 }
393
394 lseek(data->fds[plane], 0, SEEK_SET);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700395 if (plane == bo->meta.num_planes - 1 || data->offsets[plane + 1] == 0)
396 bo->meta.sizes[plane] = seek_end - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700397 else
Gurchetan Singh298b7572019-09-19 09:55:18 -0700398 bo->meta.sizes[plane] = data->offsets[plane + 1] - data->offsets[plane];
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700399
Gurchetan Singh298b7572019-09-19 09:55:18 -0700400 if ((int64_t)bo->meta.offsets[plane] + bo->meta.sizes[plane] > seek_end) {
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700401 drv_log("buffer size is too large.\n");
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700402 goto destroy_bo;
Daniel Hung-yu Wu9607a482017-09-12 20:05:08 +0800403 }
404
Gurchetan Singh298b7572019-09-19 09:55:18 -0700405 bo->meta.total_size += bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700406 }
407
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700408 return bo;
Gurchetan Singhc26fd1e2017-09-29 10:18:59 -0700409
410destroy_bo:
411 drv_bo_destroy(bo);
412 return NULL;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700413}
414
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800415void *drv_bo_map(struct bo *bo, const struct rectangle *rect, uint32_t map_flags,
416 struct mapping **map_data, size_t plane)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700417{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700418 uint32_t i;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700419 uint8_t *addr;
Gurchetan Singh99644382020-10-07 15:28:11 -0700420 struct mapping mapping = { 0 };
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700421
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800422 assert(rect->width >= 0);
423 assert(rect->height >= 0);
424 assert(rect->x + rect->width <= drv_bo_get_width(bo));
425 assert(rect->y + rect->height <= drv_bo_get_height(bo));
Gurchetan Singhf7f633a2017-09-28 17:02:12 -0700426 assert(BO_MAP_READ_WRITE & map_flags);
Tomasz Figae0807b12017-08-04 12:50:03 +0900427 /* No CPU access for protected buffers. */
Gurchetan Singh298b7572019-09-19 09:55:18 -0700428 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700429
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800430 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000431 return MAP_FAILED;
David Stevens26fe6822020-03-09 12:23:42 +0000432
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800433 mapping.rect = *rect;
434 mapping.refcount = 1;
435
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800436 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700437
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700438 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
439 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
440 if (prior->vma->handle != bo->handles[plane].u32 ||
441 prior->vma->map_flags != map_flags)
442 continue;
443
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800444 if (rect->x != prior->rect.x || rect->y != prior->rect.y ||
445 rect->width != prior->rect.width || rect->height != prior->rect.height)
446 continue;
447
448 prior->refcount++;
449 *map_data = prior;
450 goto exact_match;
451 }
452
453 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
454 struct mapping *prior = (struct mapping *)drv_array_at_idx(bo->drv->mappings, i);
455 if (prior->vma->handle != bo->handles[plane].u32 ||
456 prior->vma->map_flags != map_flags)
457 continue;
458
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700459 prior->vma->refcount++;
460 mapping.vma = prior->vma;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700461 goto success;
462 }
463
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700464 mapping.vma = calloc(1, sizeof(*mapping.vma));
Gurchetan Singh298b7572019-09-19 09:55:18 -0700465 memcpy(mapping.vma->map_strides, bo->meta.strides, sizeof(mapping.vma->map_strides));
Gurchetan Singhee43c302017-11-14 18:20:27 -0800466 addr = bo->drv->backend->bo_map(bo, mapping.vma, plane, map_flags);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700467 if (addr == MAP_FAILED) {
468 *map_data = NULL;
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700469 free(mapping.vma);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800470 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700471 return MAP_FAILED;
472 }
473
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700474 mapping.vma->refcount = 1;
475 mapping.vma->addr = addr;
476 mapping.vma->handle = bo->handles[plane].u32;
477 mapping.vma->map_flags = map_flags;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700478
479success:
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700480 *map_data = drv_array_append(bo->drv->mappings, &mapping);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800481exact_match:
482 drv_bo_invalidate(bo, *map_data);
483 addr = (uint8_t *)((*map_data)->vma->addr);
484 addr += drv_bo_get_plane_offset(bo, plane);
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800485 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800486 return (void *)addr;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700487}
488
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700489int drv_bo_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700490{
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700491 uint32_t i;
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700492 int ret = 0;
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700493
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800494 pthread_mutex_lock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700495
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800496 if (--mapping->refcount)
497 goto out;
498
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700499 if (!--mapping->vma->refcount) {
Gurchetan Singhee43c302017-11-14 18:20:27 -0800500 ret = bo->drv->backend->bo_unmap(bo, mapping->vma);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700501 free(mapping->vma);
Gurchetan Singhcfedbcc2017-11-02 17:32:00 -0700502 }
503
504 for (i = 0; i < drv_array_size(bo->drv->mappings); i++) {
505 if (mapping == (struct mapping *)drv_array_at_idx(bo->drv->mappings, i)) {
506 drv_array_remove(bo->drv->mappings, i);
507 break;
508 }
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700509 }
510
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800511out:
Gurchetan Singh27dd4702016-11-23 17:27:52 -0800512 pthread_mutex_unlock(&bo->drv->driver_lock);
Gurchetan Singh1a31e602016-10-06 10:58:00 -0700513 return ret;
514}
515
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700516int drv_bo_invalidate(struct bo *bo, struct mapping *mapping)
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700517{
518 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700519
520 assert(mapping);
521 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800522 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700523 assert(mapping->vma->refcount > 0);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700524
525 if (bo->drv->backend->bo_invalidate)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700526 ret = bo->drv->backend->bo_invalidate(bo, mapping);
Gurchetan Singhc2ad63e2017-10-09 17:59:47 -0700527
528 return ret;
529}
530
Jason Macnak1de7f662020-01-24 15:05:57 -0800531int drv_bo_flush(struct bo *bo, struct mapping *mapping)
532{
533 int ret = 0;
534
535 assert(mapping);
536 assert(mapping->vma);
537 assert(mapping->refcount > 0);
538 assert(mapping->vma->refcount > 0);
539
540 if (bo->drv->backend->bo_flush)
541 ret = bo->drv->backend->bo_flush(bo, mapping);
542
543 return ret;
544}
545
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700546int drv_bo_flush_or_unmap(struct bo *bo, struct mapping *mapping)
Gurchetan Singhff741412017-09-13 17:54:36 -0700547{
548 int ret = 0;
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700549
550 assert(mapping);
551 assert(mapping->vma);
Gurchetan Singh1ef809e2017-11-06 11:07:52 -0800552 assert(mapping->refcount > 0);
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700553 assert(mapping->vma->refcount > 0);
Gurchetan Singh298b7572019-09-19 09:55:18 -0700554 assert(!(bo->meta.use_flags & BO_USE_PROTECTED));
Gurchetan Singhff741412017-09-13 17:54:36 -0700555
556 if (bo->drv->backend->bo_flush)
Gurchetan Singh47e629b2017-11-02 14:07:18 -0700557 ret = bo->drv->backend->bo_flush(bo, mapping);
Gurchetan Singhbd1b1b52018-03-29 16:34:53 -0700558 else
559 ret = drv_bo_unmap(bo, mapping);
Gurchetan Singhff741412017-09-13 17:54:36 -0700560
561 return ret;
562}
563
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700564uint32_t drv_bo_get_width(struct bo *bo)
565{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700566 return bo->meta.width;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700567}
568
569uint32_t drv_bo_get_height(struct bo *bo)
570{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700571 return bo->meta.height;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700572}
573
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700574size_t drv_bo_get_num_planes(struct bo *bo)
575{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700576 return bo->meta.num_planes;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700577}
578
579union bo_handle drv_bo_get_plane_handle(struct bo *bo, size_t plane)
580{
581 return bo->handles[plane];
582}
583
584#ifndef DRM_RDWR
585#define DRM_RDWR O_RDWR
586#endif
587
588int drv_bo_get_plane_fd(struct bo *bo, size_t plane)
589{
590
591 int ret, fd;
Gurchetan Singh298b7572019-09-19 09:55:18 -0700592 assert(plane < bo->meta.num_planes);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700593
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800594 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000595 return -EINVAL;
David Stevens26fe6822020-03-09 12:23:42 +0000596
Gurchetan Singh1b1d56a2017-03-10 16:25:23 -0800597 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC | DRM_RDWR, &fd);
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700598
Alistair Strachanf048a1e2018-03-20 11:10:51 -0700599 // Older DRM implementations blocked DRM_RDWR, but gave a read/write mapping anyways
600 if (ret)
601 ret = drmPrimeHandleToFD(bo->drv->fd, bo->handles[plane].u32, DRM_CLOEXEC, &fd);
602
Jason Macnak166fe142021-01-29 07:50:34 -0800603 if (ret)
604 drv_log("Failed to get plane fd: %s\n", strerror(errno));
605
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700606 return (ret) ? ret : fd;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700607}
608
609uint32_t drv_bo_get_plane_offset(struct bo *bo, size_t plane)
610{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700611 assert(plane < bo->meta.num_planes);
612 return bo->meta.offsets[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700613}
614
615uint32_t drv_bo_get_plane_size(struct bo *bo, size_t plane)
616{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700617 assert(plane < bo->meta.num_planes);
618 return bo->meta.sizes[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700619}
620
621uint32_t drv_bo_get_plane_stride(struct bo *bo, size_t plane)
622{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700623 assert(plane < bo->meta.num_planes);
624 return bo->meta.strides[plane];
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700625}
626
Gurchetan Singh52155b42021-01-27 17:55:17 -0800627uint64_t drv_bo_get_format_modifier(struct bo *bo)
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700628{
Gurchetan Singh52155b42021-01-27 17:55:17 -0800629 return bo->meta.format_modifier;
Gurchetan Singh46faf6b2016-08-05 14:40:07 -0700630}
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700631
Gurchetan Singhf3b22da2016-11-21 10:46:38 -0800632uint32_t drv_bo_get_format(struct bo *bo)
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700633{
Gurchetan Singh298b7572019-09-19 09:55:18 -0700634 return bo->meta.format;
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700635}
636
Jason Macnak1de7f662020-01-24 15:05:57 -0800637size_t drv_bo_get_total_size(struct bo *bo)
638{
639 return bo->meta.total_size;
640}
641
Gurchetan Singha1892b22017-09-28 16:40:52 -0700642uint32_t drv_resolve_format(struct driver *drv, uint32_t format, uint64_t use_flags)
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700643{
644 if (drv->backend->resolve_format)
Gurchetan Singh0d44d482019-06-04 19:39:51 -0700645 return drv->backend->resolve_format(drv, format, use_flags);
Gurchetan Singhbfba8c22016-08-16 17:57:10 -0700646
647 return format;
648}
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700649
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700650uint32_t drv_num_buffers_per_bo(struct bo *bo)
651{
652 uint32_t count = 0;
653 size_t plane, p;
654
Gurchetan Singhcadc54f2021-02-01 12:03:11 -0800655 if (bo->is_test_buffer)
David Stevens26fe6822020-03-09 12:23:42 +0000656 return 0;
David Stevens26fe6822020-03-09 12:23:42 +0000657
Gurchetan Singh298b7572019-09-19 09:55:18 -0700658 for (plane = 0; plane < bo->meta.num_planes; plane++) {
Gurchetan Singh2e786ad2016-08-24 18:31:23 -0700659 for (p = 0; p < plane; p++)
660 if (bo->handles[p].u32 == bo->handles[plane].u32)
661 break;
662 if (p == plane)
663 count++;
664 }
665
666 return count;
667}
Alistair Strachan0cfaaa52018-03-19 14:03:23 -0700668
669void drv_log_prefix(const char *prefix, const char *file, int line, const char *format, ...)
670{
671 char buf[50];
672 snprintf(buf, sizeof(buf), "[%s:%s(%d)]", prefix, basename(file), line);
673
674 va_list args;
675 va_start(args, format);
676#ifdef __ANDROID__
677 __android_log_vprint(ANDROID_LOG_ERROR, buf, format, args);
678#else
679 fprintf(stderr, "%s ", buf);
680 vfprintf(stderr, format, args);
681#endif
682 va_end(args);
683}
Gurchetan Singhbc4f0232019-06-27 20:05:54 -0700684
685int drv_resource_info(struct bo *bo, uint32_t strides[DRV_MAX_PLANES],
686 uint32_t offsets[DRV_MAX_PLANES])
687{
688 for (uint32_t plane = 0; plane < bo->meta.num_planes; plane++) {
689 strides[plane] = bo->meta.strides[plane];
690 offsets[plane] = bo->meta.offsets[plane];
691 }
692
693 if (bo->drv->backend->resource_info)
694 return bo->drv->backend->resource_info(bo, strides, offsets);
695
696 return 0;
697}